michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * Test for bug 395739 to make sure the feedback to the search results in those michael@0: * entries getting better ranks. Additionally, exact matches should be ranked michael@0: * higher. Because the interactions among adaptive rank and visit counts is not michael@0: * well defined, this test holds one of the two values constant when modifying michael@0: * the other. michael@0: * michael@0: * This also tests bug 395735 for the instrumentation feedback mechanism. michael@0: * michael@0: * Bug 411293 is tested to make sure the drop down strongly prefers previously michael@0: * typed pages that have been selected and are moved to the top with adaptive michael@0: * learning. michael@0: */ michael@0: michael@0: function AutoCompleteInput(aSearches) { michael@0: this.searches = aSearches; michael@0: } michael@0: AutoCompleteInput.prototype = { michael@0: constructor: AutoCompleteInput, michael@0: michael@0: get minResultsForPopup() 0, michael@0: get timeout() 10, michael@0: get searchParam() "", michael@0: get textValue() "", michael@0: get disableAutoComplete() false, michael@0: get completeDefaultIndex() false, michael@0: michael@0: get searchCount() this.searches.length, michael@0: getSearchAt: function (aIndex) this.searches[aIndex], michael@0: michael@0: onSearchBegin: function () {}, michael@0: onSearchComplete: function() {}, michael@0: michael@0: get popupOpen() false, michael@0: popup: { michael@0: set selectedIndex(aIndex) aIndex, michael@0: invalidate: function () {}, michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompletePopup]) michael@0: }, michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteInput]) michael@0: } michael@0: michael@0: /** michael@0: * Checks that autocomplete results are ordered correctly. michael@0: */ michael@0: function ensure_results(expected, searchTerm) michael@0: { michael@0: let controller = Cc["@mozilla.org/autocomplete/controller;1"]. michael@0: getService(Ci.nsIAutoCompleteController); michael@0: michael@0: // Make an AutoCompleteInput that uses our searches michael@0: // and confirms results on search complete. michael@0: let input = new AutoCompleteInput(["history"]); michael@0: michael@0: controller.input = input; michael@0: michael@0: input.onSearchComplete = function() { michael@0: do_check_eq(controller.searchStatus, michael@0: Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH); michael@0: do_check_eq(controller.matchCount, expected.length); michael@0: for (let i = 0; i < controller.matchCount; i++) { michael@0: print("Testing for '" + expected[i].uri.spec + "' got '" + controller.getValueAt(i) + "'"); michael@0: do_check_eq(controller.getValueAt(i), expected[i].uri.spec); michael@0: do_check_eq(controller.getStyleAt(i), expected[i].style); michael@0: } michael@0: michael@0: deferEnsureResults.resolve(); michael@0: }; michael@0: michael@0: controller.startSearch(searchTerm); michael@0: } michael@0: michael@0: /** michael@0: * Asynchronous task that bumps up the rank for an uri. michael@0: */ michael@0: function task_setCountRank(aURI, aCount, aRank, aSearch, aBookmark) michael@0: { michael@0: // Bump up the visit count for the uri. michael@0: let visits = []; michael@0: for (let i = 0; i < aCount; i++) { michael@0: visits.push({ uri: aURI, visitDate: d1, transition: TRANSITION_TYPED }); michael@0: } michael@0: yield promiseAddVisits(visits); michael@0: michael@0: // Make a nsIAutoCompleteController and friends for instrumentation feedback. michael@0: let thing = { michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteInput, michael@0: Ci.nsIAutoCompletePopup, michael@0: Ci.nsIAutoCompleteController]), michael@0: get popup() thing, michael@0: get controller() thing, michael@0: popupOpen: true, michael@0: selectedIndex: 0, michael@0: getValueAt: function() aURI.spec, michael@0: searchString: aSearch michael@0: }; michael@0: michael@0: // Bump up the instrumentation feedback. michael@0: for (let i = 0; i < aRank; i++) { michael@0: Services.obs.notifyObservers(thing, "autocomplete-will-enter-text", null); michael@0: } michael@0: michael@0: // If this is supposed to be a bookmark, add it. michael@0: if (aBookmark) { michael@0: PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, michael@0: aURI, michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: "test_book"); michael@0: michael@0: // And add the tag if we need to. michael@0: if (aBookmark == "tag") { michael@0: PlacesUtils.tagging.tagURI(aURI, ["test_tag"]); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Decay the adaptive entries by sending the daily idle topic. michael@0: */ michael@0: function doAdaptiveDecay() michael@0: { michael@0: PlacesUtils.history.runInBatchMode({ michael@0: runBatched: function() { michael@0: for (let i = 0; i < 10; i++) { michael@0: PlacesUtils.history.QueryInterface(Ci.nsIObserver) michael@0: .observe(null, "idle-daily", null); michael@0: } michael@0: } michael@0: }, this); michael@0: } michael@0: michael@0: let uri1 = uri("http://site.tld/1"); michael@0: let uri2 = uri("http://site.tld/2"); michael@0: michael@0: // d1 is some date for the page visit michael@0: let d1 = new Date(Date.now() - 1000 * 60 * 60) * 1000; michael@0: // c1 is larger (should show up higher) than c2 michael@0: let c1 = 10; michael@0: let c2 = 1; michael@0: // s1 is a partial match of s2 michael@0: let s0 = ""; michael@0: let s1 = "si"; michael@0: let s2 = "site"; michael@0: michael@0: let observer = { michael@0: results: null, michael@0: search: null, michael@0: runCount: -1, michael@0: observe: function(aSubject, aTopic, aData) michael@0: { michael@0: if (--this.runCount > 0) michael@0: return; michael@0: ensure_results(this.results, this.search); michael@0: } michael@0: }; michael@0: Services.obs.addObserver(observer, PlacesUtils.TOPIC_FEEDBACK_UPDATED, false); michael@0: michael@0: /** michael@0: * Make the result object for a given URI that will be passed to ensure_results. michael@0: */ michael@0: function makeResult(aURI) { michael@0: return { michael@0: uri: aURI, michael@0: style: "favicon", michael@0: }; michael@0: } michael@0: michael@0: let tests = [ michael@0: // Test things without a search term. michael@0: function() { michael@0: print("Test 0 same count, diff rank, same term; no search"); michael@0: observer.results = [ michael@0: makeResult(uri1), michael@0: makeResult(uri2), michael@0: ]; michael@0: observer.search = s0; michael@0: observer.runCount = c1 + c2; michael@0: yield task_setCountRank(uri1, c1, c1, s2); michael@0: yield task_setCountRank(uri2, c1, c2, s2); michael@0: }, michael@0: function() { michael@0: print("Test 1 same count, diff rank, same term; no search"); michael@0: observer.results = [ michael@0: makeResult(uri2), michael@0: makeResult(uri1), michael@0: ]; michael@0: observer.search = s0; michael@0: observer.runCount = c1 + c2; michael@0: yield task_setCountRank(uri1, c1, c2, s2); michael@0: yield task_setCountRank(uri2, c1, c1, s2); michael@0: }, michael@0: function() { michael@0: print("Test 2 diff count, same rank, same term; no search"); michael@0: observer.results = [ michael@0: makeResult(uri1), michael@0: makeResult(uri2), michael@0: ]; michael@0: observer.search = s0; michael@0: observer.runCount = c1 + c1; michael@0: yield task_setCountRank(uri1, c1, c1, s2); michael@0: yield task_setCountRank(uri2, c2, c1, s2); michael@0: }, michael@0: function() { michael@0: print("Test 3 diff count, same rank, same term; no search"); michael@0: observer.results = [ michael@0: makeResult(uri2), michael@0: makeResult(uri1), michael@0: ]; michael@0: observer.search = s0; michael@0: observer.runCount = c1 + c1; michael@0: yield task_setCountRank(uri1, c2, c1, s2); michael@0: yield task_setCountRank(uri2, c1, c1, s2); michael@0: }, michael@0: michael@0: // Test things with a search term (exact match one, partial other). michael@0: function() { michael@0: print("Test 4 same count, same rank, diff term; one exact/one partial search"); michael@0: observer.results = [ michael@0: makeResult(uri1), michael@0: makeResult(uri2), michael@0: ]; michael@0: observer.search = s1; michael@0: observer.runCount = c1 + c1; michael@0: yield task_setCountRank(uri1, c1, c1, s1); michael@0: yield task_setCountRank(uri2, c1, c1, s2); michael@0: }, michael@0: function() { michael@0: print("Test 5 same count, same rank, diff term; one exact/one partial search"); michael@0: observer.results = [ michael@0: makeResult(uri2), michael@0: makeResult(uri1), michael@0: ]; michael@0: observer.search = s1; michael@0: observer.runCount = c1 + c1; michael@0: yield task_setCountRank(uri1, c1, c1, s2); michael@0: yield task_setCountRank(uri2, c1, c1, s1); michael@0: }, michael@0: michael@0: // Test things with a search term (exact match both). michael@0: function() { michael@0: print("Test 6 same count, diff rank, same term; both exact search"); michael@0: observer.results = [ michael@0: makeResult(uri1), michael@0: makeResult(uri2), michael@0: ]; michael@0: observer.search = s1; michael@0: observer.runCount = c1 + c2; michael@0: yield task_setCountRank(uri1, c1, c1, s1); michael@0: yield task_setCountRank(uri2, c1, c2, s1); michael@0: }, michael@0: function() { michael@0: print("Test 7 same count, diff rank, same term; both exact search"); michael@0: observer.results = [ michael@0: makeResult(uri2), michael@0: makeResult(uri1), michael@0: ]; michael@0: observer.search = s1; michael@0: observer.runCount = c1 + c2; michael@0: yield task_setCountRank(uri1, c1, c2, s1); michael@0: yield task_setCountRank(uri2, c1, c1, s1); michael@0: }, michael@0: michael@0: // Test things with a search term (partial match both). michael@0: function() { michael@0: print("Test 8 same count, diff rank, same term; both partial search"); michael@0: observer.results = [ michael@0: makeResult(uri1), michael@0: makeResult(uri2), michael@0: ]; michael@0: observer.search = s1; michael@0: observer.runCount = c1 + c2; michael@0: yield task_setCountRank(uri1, c1, c1, s2); michael@0: yield task_setCountRank(uri2, c1, c2, s2); michael@0: }, michael@0: function() { michael@0: print("Test 9 same count, diff rank, same term; both partial search"); michael@0: observer.results = [ michael@0: makeResult(uri2), michael@0: makeResult(uri1), michael@0: ]; michael@0: observer.search = s1; michael@0: observer.runCount = c1 + c2; michael@0: yield task_setCountRank(uri1, c1, c2, s2); michael@0: yield task_setCountRank(uri2, c1, c1, s2); michael@0: }, michael@0: function() { michael@0: print("Test 10 same count, same rank, same term, decay first; exact match"); michael@0: observer.results = [ michael@0: makeResult(uri2), michael@0: makeResult(uri1), michael@0: ]; michael@0: observer.search = s1; michael@0: observer.runCount = c1 + c1; michael@0: yield task_setCountRank(uri1, c1, c1, s1); michael@0: doAdaptiveDecay(); michael@0: yield task_setCountRank(uri2, c1, c1, s1); michael@0: }, michael@0: function() { michael@0: print("Test 11 same count, same rank, same term, decay second; exact match"); michael@0: observer.results = [ michael@0: makeResult(uri1), michael@0: makeResult(uri2), michael@0: ]; michael@0: observer.search = s1; michael@0: observer.runCount = c1 + c1; michael@0: yield task_setCountRank(uri2, c1, c1, s1); michael@0: doAdaptiveDecay(); michael@0: yield task_setCountRank(uri1, c1, c1, s1); michael@0: }, michael@0: // Test that bookmarks or tags are hidden if the preferences are set right. michael@0: function() { michael@0: print("Test 12 same count, diff rank, same term; no search; history only"); michael@0: Services.prefs.setIntPref("browser.urlbar.matchBehavior", michael@0: Ci.mozIPlacesAutoComplete.BEHAVIOR_HISTORY); michael@0: observer.results = [ michael@0: makeResult(uri1), michael@0: makeResult(uri2), michael@0: ]; michael@0: observer.search = s0; michael@0: observer.runCount = c1 + c2; michael@0: yield task_setCountRank(uri1, c1, c1, s2, "bookmark"); michael@0: yield task_setCountRank(uri2, c1, c2, s2); michael@0: }, michael@0: function() { michael@0: print("Test 13 same count, diff rank, same term; no search; history only with tag"); michael@0: Services.prefs.setIntPref("browser.urlbar.matchBehavior", michael@0: Ci.mozIPlacesAutoComplete.BEHAVIOR_HISTORY); michael@0: observer.results = [ michael@0: makeResult(uri1), michael@0: makeResult(uri2), michael@0: ]; michael@0: observer.search = s0; michael@0: observer.runCount = c1 + c2; michael@0: yield task_setCountRank(uri1, c1, c1, s2, "tag"); michael@0: yield task_setCountRank(uri2, c1, c2, s2); michael@0: }, michael@0: ]; michael@0: michael@0: /** michael@0: * This deferred object contains a promise that is resolved when the michael@0: * ensure_results function has finished its execution. michael@0: */ michael@0: let deferEnsureResults; michael@0: michael@0: /** michael@0: * Test adaptive autocomplete. michael@0: */ michael@0: function run_test() michael@0: { michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_task(function test_adaptive() michael@0: { michael@0: for (let [, test] in Iterator(tests)) { michael@0: // Cleanup. michael@0: PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.unfiledBookmarksFolderId); michael@0: PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.tagsFolderId); michael@0: observer.runCount = -1; michael@0: michael@0: yield promiseClearHistory(); michael@0: michael@0: deferEnsureResults = Promise.defer(); michael@0: yield test(); michael@0: yield deferEnsureResults.promise; michael@0: } michael@0: michael@0: Services.obs.removeObserver(observer, PlacesUtils.TOPIC_FEEDBACK_UPDATED); michael@0: });