michael@0: /* michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: /** michael@0: * Purpose of the test is to check that a stopSearch call comes always before a michael@0: * startSearch call. michael@0: */ michael@0: michael@0: Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: michael@0: /** michael@0: * Dummy nsIAutoCompleteInput source that returns michael@0: * the given list of AutoCompleteSearch names. michael@0: * michael@0: * Implements only the methods needed for this test. michael@0: */ michael@0: function AutoCompleteInput(aSearches) michael@0: { michael@0: this.searches = aSearches; michael@0: } michael@0: AutoCompleteInput.prototype = { michael@0: constructor: AutoCompleteInput, michael@0: minResultsForPopup: 0, michael@0: timeout: 10, michael@0: searchParam: "", michael@0: textValue: "hello", michael@0: disableAutoComplete: false, michael@0: completeDefaultIndex: false, michael@0: set popupOpen(val) { return val; }, // ignore michael@0: get popupOpen() { return false; }, michael@0: get searchCount() { return this.searches.length; }, michael@0: getSearchAt: function(aIndex) { return this.searches[aIndex]; }, michael@0: onSearchBegin: function() {}, michael@0: onSearchComplete: function() {}, michael@0: onTextReverted: function () {}, michael@0: onTextEntered: function () {}, michael@0: popup: { michael@0: selectBy: function() {}, michael@0: invalidate: function() {}, michael@0: set selectedIndex(val) { return val; }, // ignore michael@0: get selectedIndex() { return -1 }, michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompletePopup]) michael@0: }, michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteInput]) michael@0: } michael@0: michael@0: michael@0: /** michael@0: * nsIAutoCompleteSearch implementation. michael@0: */ michael@0: function AutoCompleteSearch(aName) michael@0: { michael@0: this.name = aName; michael@0: } michael@0: AutoCompleteSearch.prototype = { michael@0: constructor: AutoCompleteSearch, michael@0: stopSearchInvoked: true, michael@0: startSearch: function(aSearchString, aSearchParam, aPreviousResult, aListener) michael@0: { michael@0: print("Check stop search has been called"); michael@0: do_check_true(this.stopSearchInvoked); michael@0: this.stopSearchInvoked = false; michael@0: }, michael@0: stopSearch: function() michael@0: { michael@0: this.stopSearchInvoked = true; michael@0: }, michael@0: QueryInterface: XPCOMUtils.generateQI([ michael@0: Ci.nsIFactory michael@0: , Ci.nsIAutoCompleteSearch michael@0: ]), michael@0: createInstance: function(outer, iid) michael@0: { michael@0: return this.QueryInterface(iid); michael@0: } michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Helper to register an AutoCompleteSearch with the given name. michael@0: * Allows the AutoCompleteController to find the search. michael@0: */ michael@0: function registerAutoCompleteSearch(aSearch) michael@0: { michael@0: let name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name; michael@0: let uuidGenerator = Cc["@mozilla.org/uuid-generator;1"]. michael@0: getService(Ci.nsIUUIDGenerator); michael@0: let cid = uuidGenerator.generateUUID(); michael@0: let desc = "Test AutoCompleteSearch"; michael@0: let componentManager = Components.manager michael@0: .QueryInterface(Ci.nsIComponentRegistrar); michael@0: componentManager.registerFactory(cid, desc, name, aSearch); michael@0: // Keep the id on the object so we can unregister later michael@0: aSearch.cid = cid; michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Helper to unregister an AutoCompleteSearch. michael@0: */ michael@0: function unregisterAutoCompleteSearch(aSearch) { michael@0: let componentManager = Components.manager michael@0: .QueryInterface(Ci.nsIComponentRegistrar); michael@0: componentManager.unregisterFactory(aSearch.cid, aSearch); michael@0: } michael@0: michael@0: michael@0: let gTests = [ michael@0: function(controller) { michael@0: print("handleText"); michael@0: controller.input.textValue = "hel"; michael@0: controller.handleText(); michael@0: }, michael@0: function(controller) { michael@0: print("handleStartComposition"); michael@0: controller.handleStartComposition(); michael@0: }, michael@0: function(controller) { michael@0: print("handleEndComposition"); michael@0: controller.handleEndComposition(); michael@0: // an input event always follows compositionend event. michael@0: controller.handleText(); michael@0: }, michael@0: function(controller) { michael@0: print("handleEscape"); michael@0: controller.handleEscape(); michael@0: }, michael@0: function(controller) { michael@0: print("handleEnter"); michael@0: controller.handleEnter(false); michael@0: }, michael@0: function(controller) { michael@0: print("handleTab"); michael@0: controller.handleTab(); michael@0: }, michael@0: michael@0: function(controller) { michael@0: print("handleKeyNavigation"); michael@0: controller.handleKeyNavigation(Ci.nsIDOMKeyEvent.DOM_VK_UP); michael@0: }, michael@0: ]; michael@0: michael@0: michael@0: let gSearch; michael@0: let gCurrentTest; michael@0: function run_test() { michael@0: // Make an AutoCompleteSearch that always returns nothing michael@0: gSearch = new AutoCompleteSearch("test"); michael@0: registerAutoCompleteSearch(gSearch); 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 search. michael@0: let input = new AutoCompleteInput([gSearch.name]); michael@0: controller.input = input; michael@0: michael@0: input.onSearchBegin = function() { michael@0: do_execute_soon(function() { michael@0: gCurrentTest(controller); michael@0: }); michael@0: }; michael@0: input.onSearchComplete = function() { michael@0: run_next_test(controller); michael@0: } michael@0: michael@0: // Search is asynchronous, so don't let the test finish immediately michael@0: do_test_pending(); michael@0: michael@0: run_next_test(controller); michael@0: } michael@0: michael@0: function run_next_test(controller) { michael@0: if (gTests.length == 0) { michael@0: unregisterAutoCompleteSearch(gSearch); michael@0: controller.stopSearch(); michael@0: controller.input = null; michael@0: do_test_finished(); michael@0: return; michael@0: } michael@0: michael@0: gCurrentTest = gTests.shift(); michael@0: controller.startSearch("hello"); michael@0: }