Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 | const TEST_URL = "http://adapt.mozilla.org/"; |
michael@0 | 8 | const SEARCH_STRING = "adapt"; |
michael@0 | 9 | |
michael@0 | 10 | Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 11 | |
michael@0 | 12 | let hs = Cc["@mozilla.org/browser/nav-history-service;1"]. |
michael@0 | 13 | getService(Ci.nsINavHistoryService); |
michael@0 | 14 | let bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]. |
michael@0 | 15 | getService(Ci.nsINavBookmarksService); |
michael@0 | 16 | let os = Cc["@mozilla.org/observer-service;1"]. |
michael@0 | 17 | getService(Ci.nsIObserverService); |
michael@0 | 18 | let ps = Cc["@mozilla.org/preferences-service;1"]. |
michael@0 | 19 | getService(Ci.nsIPrefBranch); |
michael@0 | 20 | |
michael@0 | 21 | const PLACES_AUTOCOMPLETE_FEEDBACK_UPDATED_TOPIC = |
michael@0 | 22 | "places-autocomplete-feedback-updated"; |
michael@0 | 23 | |
michael@0 | 24 | function AutoCompleteInput(aSearches) { |
michael@0 | 25 | this.searches = aSearches; |
michael@0 | 26 | } |
michael@0 | 27 | AutoCompleteInput.prototype = { |
michael@0 | 28 | constructor: AutoCompleteInput, |
michael@0 | 29 | searches: null, |
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 ACI_getSearchAt(aIndex) { |
michael@0 | 42 | return this.searches[aIndex]; |
michael@0 | 43 | }, |
michael@0 | 44 | |
michael@0 | 45 | onSearchComplete: function ACI_onSearchComplete() {}, |
michael@0 | 46 | |
michael@0 | 47 | popupOpen: false, |
michael@0 | 48 | |
michael@0 | 49 | popup: { |
michael@0 | 50 | setSelectedIndex: function() {}, |
michael@0 | 51 | invalidate: function() {}, |
michael@0 | 52 | |
michael@0 | 53 | QueryInterface: function(iid) { |
michael@0 | 54 | if (iid.equals(Ci.nsISupports) || |
michael@0 | 55 | iid.equals(Ci.nsIAutoCompletePopup)) |
michael@0 | 56 | return this; |
michael@0 | 57 | |
michael@0 | 58 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 59 | } |
michael@0 | 60 | }, |
michael@0 | 61 | |
michael@0 | 62 | onSearchBegin: function() {}, |
michael@0 | 63 | |
michael@0 | 64 | QueryInterface: function(iid) { |
michael@0 | 65 | if (iid.equals(Ci.nsISupports) || |
michael@0 | 66 | iid.equals(Ci.nsIAutoCompleteInput)) |
michael@0 | 67 | return this; |
michael@0 | 68 | |
michael@0 | 69 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 70 | } |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | |
michael@0 | 74 | function check_results() { |
michael@0 | 75 | let controller = Cc["@mozilla.org/autocomplete/controller;1"]. |
michael@0 | 76 | getService(Ci.nsIAutoCompleteController); |
michael@0 | 77 | let input = new AutoCompleteInput(["history"]); |
michael@0 | 78 | controller.input = input; |
michael@0 | 79 | |
michael@0 | 80 | input.onSearchComplete = function() { |
michael@0 | 81 | do_check_eq(controller.searchStatus, |
michael@0 | 82 | Ci.nsIAutoCompleteController.STATUS_COMPLETE_NO_MATCH); |
michael@0 | 83 | do_check_eq(controller.matchCount, 0); |
michael@0 | 84 | |
michael@0 | 85 | remove_all_bookmarks(); |
michael@0 | 86 | do_test_finished(); |
michael@0 | 87 | }; |
michael@0 | 88 | |
michael@0 | 89 | controller.startSearch(SEARCH_STRING); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | |
michael@0 | 93 | function addAdaptiveFeedback(aUrl, aSearch, aCallback) { |
michael@0 | 94 | let observer = { |
michael@0 | 95 | observe: function(aSubject, aTopic, aData) { |
michael@0 | 96 | os.removeObserver(observer, PLACES_AUTOCOMPLETE_FEEDBACK_UPDATED_TOPIC); |
michael@0 | 97 | do_timeout(0, aCallback); |
michael@0 | 98 | } |
michael@0 | 99 | }; |
michael@0 | 100 | os.addObserver(observer, PLACES_AUTOCOMPLETE_FEEDBACK_UPDATED_TOPIC, false); |
michael@0 | 101 | |
michael@0 | 102 | let thing = { |
michael@0 | 103 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteInput, |
michael@0 | 104 | Ci.nsIAutoCompletePopup, |
michael@0 | 105 | Ci.nsIAutoCompleteController]), |
michael@0 | 106 | get popup() { return thing; }, |
michael@0 | 107 | get controller() { return thing; }, |
michael@0 | 108 | popupOpen: true, |
michael@0 | 109 | selectedIndex: 0, |
michael@0 | 110 | getValueAt: function() aUrl, |
michael@0 | 111 | searchString: aSearch |
michael@0 | 112 | }; |
michael@0 | 113 | |
michael@0 | 114 | os.notifyObservers(thing, "autocomplete-will-enter-text", null); |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | |
michael@0 | 118 | function run_test() { |
michael@0 | 119 | do_test_pending(); |
michael@0 | 120 | |
michael@0 | 121 | // Add a bookmark to our url. |
michael@0 | 122 | bs.insertBookmark(bs.unfiledBookmarksFolder, uri(TEST_URL), |
michael@0 | 123 | bs.DEFAULT_INDEX, "test_book"); |
michael@0 | 124 | // We want to search only history. |
michael@0 | 125 | ps.setIntPref("browser.urlbar.default.behavior", |
michael@0 | 126 | Ci.mozIPlacesAutoComplete.BEHAVIOR_HISTORY); |
michael@0 | 127 | // Add an adaptive entry. |
michael@0 | 128 | addAdaptiveFeedback(TEST_URL, SEARCH_STRING, check_results); |
michael@0 | 129 | } |