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: const TEST_URL = "http://adapt.mozilla.org/"; michael@0: const SEARCH_STRING = "adapt"; michael@0: michael@0: Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: let hs = Cc["@mozilla.org/browser/nav-history-service;1"]. michael@0: getService(Ci.nsINavHistoryService); michael@0: let bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]. michael@0: getService(Ci.nsINavBookmarksService); michael@0: let os = Cc["@mozilla.org/observer-service;1"]. michael@0: getService(Ci.nsIObserverService); michael@0: let ps = Cc["@mozilla.org/preferences-service;1"]. michael@0: getService(Ci.nsIPrefBranch); michael@0: michael@0: const PLACES_AUTOCOMPLETE_FEEDBACK_UPDATED_TOPIC = michael@0: "places-autocomplete-feedback-updated"; michael@0: michael@0: function AutoCompleteInput(aSearches) { michael@0: this.searches = aSearches; michael@0: } michael@0: AutoCompleteInput.prototype = { michael@0: constructor: AutoCompleteInput, michael@0: searches: null, michael@0: minResultsForPopup: 0, michael@0: timeout: 10, michael@0: searchParam: "", michael@0: textValue: "", michael@0: disableAutoComplete: false, michael@0: completeDefaultIndex: false, michael@0: michael@0: get searchCount() { michael@0: return this.searches.length; michael@0: }, michael@0: michael@0: getSearchAt: function ACI_getSearchAt(aIndex) { michael@0: return this.searches[aIndex]; michael@0: }, michael@0: michael@0: onSearchComplete: function ACI_onSearchComplete() {}, michael@0: michael@0: popupOpen: false, michael@0: michael@0: popup: { michael@0: setSelectedIndex: function() {}, michael@0: invalidate: function() {}, michael@0: michael@0: QueryInterface: function(iid) { michael@0: if (iid.equals(Ci.nsISupports) || michael@0: iid.equals(Ci.nsIAutoCompletePopup)) michael@0: return this; michael@0: michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: } michael@0: }, michael@0: michael@0: onSearchBegin: function() {}, michael@0: michael@0: QueryInterface: function(iid) { michael@0: if (iid.equals(Ci.nsISupports) || michael@0: iid.equals(Ci.nsIAutoCompleteInput)) michael@0: return this; michael@0: michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: } michael@0: } michael@0: michael@0: michael@0: function check_results() { michael@0: let controller = Cc["@mozilla.org/autocomplete/controller;1"]. michael@0: getService(Ci.nsIAutoCompleteController); michael@0: let input = new AutoCompleteInput(["history"]); 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_NO_MATCH); michael@0: do_check_eq(controller.matchCount, 0); michael@0: michael@0: remove_all_bookmarks(); michael@0: do_test_finished(); michael@0: }; michael@0: michael@0: controller.startSearch(SEARCH_STRING); michael@0: } michael@0: michael@0: michael@0: function addAdaptiveFeedback(aUrl, aSearch, aCallback) { michael@0: let observer = { michael@0: observe: function(aSubject, aTopic, aData) { michael@0: os.removeObserver(observer, PLACES_AUTOCOMPLETE_FEEDBACK_UPDATED_TOPIC); michael@0: do_timeout(0, aCallback); michael@0: } michael@0: }; michael@0: os.addObserver(observer, PLACES_AUTOCOMPLETE_FEEDBACK_UPDATED_TOPIC, false); michael@0: michael@0: let thing = { michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteInput, michael@0: Ci.nsIAutoCompletePopup, michael@0: Ci.nsIAutoCompleteController]), michael@0: get popup() { return thing; }, michael@0: get controller() { return thing; }, michael@0: popupOpen: true, michael@0: selectedIndex: 0, michael@0: getValueAt: function() aUrl, michael@0: searchString: aSearch michael@0: }; michael@0: michael@0: os.notifyObservers(thing, "autocomplete-will-enter-text", null); michael@0: } michael@0: michael@0: michael@0: function run_test() { michael@0: do_test_pending(); michael@0: michael@0: // Add a bookmark to our url. michael@0: bs.insertBookmark(bs.unfiledBookmarksFolder, uri(TEST_URL), michael@0: bs.DEFAULT_INDEX, "test_book"); michael@0: // We want to search only history. michael@0: ps.setIntPref("browser.urlbar.default.behavior", michael@0: Ci.mozIPlacesAutoComplete.BEHAVIOR_HISTORY); michael@0: // Add an adaptive entry. michael@0: addAdaptiveFeedback(TEST_URL, SEARCH_STRING, check_results); michael@0: }