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 file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: function AutoCompleteImmediateSearch(aName, aResult) { michael@0: this.name = aName; michael@0: this._result = aResult; michael@0: } michael@0: AutoCompleteImmediateSearch.prototype = Object.create(AutoCompleteSearchBase.prototype); michael@0: AutoCompleteImmediateSearch.prototype.searchType = michael@0: Ci.nsIAutoCompleteSearchDescriptor.SEARCH_TYPE_IMMEDIATE; michael@0: AutoCompleteImmediateSearch.prototype.QueryInterface = michael@0: XPCOMUtils.generateQI([Ci.nsIFactory, michael@0: Ci.nsIAutoCompleteSearch, michael@0: Ci.nsIAutoCompleteSearchDescriptor]); michael@0: michael@0: function AutoCompleteDelayedSearch(aName, aResult) { michael@0: this.name = aName; michael@0: this._result = aResult; michael@0: } michael@0: AutoCompleteDelayedSearch.prototype = Object.create(AutoCompleteSearchBase.prototype); michael@0: michael@0: function AutoCompleteResult(aValues, aDefaultIndex) { michael@0: this._values = aValues; michael@0: this.defaultIndex = aDefaultIndex; michael@0: } michael@0: AutoCompleteResult.prototype = Object.create(AutoCompleteResultBase.prototype); michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: } michael@0: michael@0: /** michael@0: * An immediate search should be executed synchronously. michael@0: */ michael@0: add_test(function test_immediate_search() { michael@0: let immediateResults = ["mozillaTest"]; michael@0: let inputStr = "moz"; michael@0: michael@0: let immediateSearch = new AutoCompleteImmediateSearch( michael@0: "immediate", new AutoCompleteResult(["moz-immediate"], 0)); michael@0: registerAutoCompleteSearch(immediateSearch); michael@0: let delayedSearch = new AutoCompleteDelayedSearch( michael@0: "delayed", new AutoCompleteResult(["moz-delayed"], 0)); michael@0: registerAutoCompleteSearch(delayedSearch); michael@0: michael@0: let controller = Cc["@mozilla.org/autocomplete/controller;1"]. michael@0: getService(Ci.nsIAutoCompleteController); michael@0: michael@0: let input = new AutoCompleteInputBase([delayedSearch.name, michael@0: immediateSearch.name]); michael@0: input.completeDefaultIndex = true; michael@0: input.textValue = inputStr; michael@0: michael@0: // Caret must be at the end. Autofill doesn't happen unless you're typing michael@0: // characters at the end. michael@0: let strLen = inputStr.length; michael@0: input.selectTextRange(strLen, strLen); michael@0: michael@0: controller.input = input; michael@0: controller.startSearch(inputStr); michael@0: michael@0: // Immediately check the result, the immediate search should have finished. michael@0: do_check_eq(input.textValue, "moz-immediate"); michael@0: michael@0: // Wait for both queries to finish. michael@0: input.onSearchComplete = function() { michael@0: // Sanity check. michael@0: do_check_eq(input.textValue, "moz-immediate"); michael@0: michael@0: unregisterAutoCompleteSearch(immediateSearch); michael@0: unregisterAutoCompleteSearch(delayedSearch); michael@0: run_next_test(); michael@0: }; michael@0: }); michael@0: michael@0: /** michael@0: * An immediate search should be executed before any delayed search. michael@0: */ michael@0: add_test(function test_immediate_search_notimeout() { michael@0: let immediateResults = ["mozillaTest"]; michael@0: let inputStr = "moz"; michael@0: michael@0: let immediateSearch = new AutoCompleteImmediateSearch( michael@0: "immediate", new AutoCompleteResult(["moz-immediate"], 0)); michael@0: registerAutoCompleteSearch(immediateSearch); michael@0: michael@0: let delayedSearch = new AutoCompleteDelayedSearch( michael@0: "delayed", new AutoCompleteResult(["moz-delayed"], 0)); michael@0: registerAutoCompleteSearch(delayedSearch); michael@0: michael@0: let controller = Cc["@mozilla.org/autocomplete/controller;1"]. michael@0: getService(Ci.nsIAutoCompleteController); michael@0: michael@0: let input = new AutoCompleteInputBase([delayedSearch.name, michael@0: immediateSearch.name]); michael@0: input.completeDefaultIndex = true; michael@0: input.textValue = inputStr; michael@0: input.timeout = 0; michael@0: michael@0: // Caret must be at the end. Autofill doesn't happen unless you're typing michael@0: // characters at the end. michael@0: let strLen = inputStr.length; michael@0: input.selectTextRange(strLen, strLen); michael@0: michael@0: controller.input = input; michael@0: let complete = false; michael@0: input.onSearchComplete = function() { michael@0: complete = true; michael@0: }; michael@0: controller.startSearch(inputStr); michael@0: do_check_true(complete); michael@0: michael@0: // Immediately check the result, the immediate search should have finished. michael@0: do_check_eq(input.textValue, "moz-immediate"); michael@0: michael@0: unregisterAutoCompleteSearch(immediateSearch); michael@0: unregisterAutoCompleteSearch(delayedSearch); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: /** michael@0: * A delayed search should be executed synchronously with a zero timeout. michael@0: */ michael@0: add_test(function test_delayed_search_notimeout() { michael@0: let immediateResults = ["mozillaTest"]; michael@0: let inputStr = "moz"; michael@0: michael@0: let delayedSearch = new AutoCompleteDelayedSearch( michael@0: "delayed", new AutoCompleteResult(["moz-delayed"], 0)); michael@0: registerAutoCompleteSearch(delayedSearch); michael@0: michael@0: let controller = Cc["@mozilla.org/autocomplete/controller;1"]. michael@0: getService(Ci.nsIAutoCompleteController); michael@0: michael@0: let input = new AutoCompleteInputBase([delayedSearch.name]); michael@0: input.completeDefaultIndex = true; michael@0: input.textValue = inputStr; michael@0: input.timeout = 0; michael@0: michael@0: // Caret must be at the end. Autofill doesn't happen unless you're typing michael@0: // characters at the end. michael@0: let strLen = inputStr.length; michael@0: input.selectTextRange(strLen, strLen); michael@0: michael@0: controller.input = input; michael@0: let complete = false; michael@0: input.onSearchComplete = function() { michael@0: complete = true; michael@0: }; michael@0: controller.startSearch(inputStr); michael@0: do_check_true(complete); michael@0: michael@0: // Immediately check the result, the delayed search should have finished. michael@0: do_check_eq(input.textValue, "moz-delayed"); michael@0: michael@0: unregisterAutoCompleteSearch(delayedSearch); michael@0: run_next_test(); michael@0: });