1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/autocomplete/tests/unit/test_immediate_search.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,160 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 + 1.9 +function AutoCompleteImmediateSearch(aName, aResult) { 1.10 + this.name = aName; 1.11 + this._result = aResult; 1.12 +} 1.13 +AutoCompleteImmediateSearch.prototype = Object.create(AutoCompleteSearchBase.prototype); 1.14 +AutoCompleteImmediateSearch.prototype.searchType = 1.15 + Ci.nsIAutoCompleteSearchDescriptor.SEARCH_TYPE_IMMEDIATE; 1.16 +AutoCompleteImmediateSearch.prototype.QueryInterface = 1.17 + XPCOMUtils.generateQI([Ci.nsIFactory, 1.18 + Ci.nsIAutoCompleteSearch, 1.19 + Ci.nsIAutoCompleteSearchDescriptor]); 1.20 + 1.21 +function AutoCompleteDelayedSearch(aName, aResult) { 1.22 + this.name = aName; 1.23 + this._result = aResult; 1.24 +} 1.25 +AutoCompleteDelayedSearch.prototype = Object.create(AutoCompleteSearchBase.prototype); 1.26 + 1.27 +function AutoCompleteResult(aValues, aDefaultIndex) { 1.28 + this._values = aValues; 1.29 + this.defaultIndex = aDefaultIndex; 1.30 +} 1.31 +AutoCompleteResult.prototype = Object.create(AutoCompleteResultBase.prototype); 1.32 + 1.33 +function run_test() { 1.34 + run_next_test(); 1.35 +} 1.36 + 1.37 +/** 1.38 + * An immediate search should be executed synchronously. 1.39 + */ 1.40 +add_test(function test_immediate_search() { 1.41 + let immediateResults = ["mozillaTest"]; 1.42 + let inputStr = "moz"; 1.43 + 1.44 + let immediateSearch = new AutoCompleteImmediateSearch( 1.45 + "immediate", new AutoCompleteResult(["moz-immediate"], 0)); 1.46 + registerAutoCompleteSearch(immediateSearch); 1.47 + let delayedSearch = new AutoCompleteDelayedSearch( 1.48 + "delayed", new AutoCompleteResult(["moz-delayed"], 0)); 1.49 + registerAutoCompleteSearch(delayedSearch); 1.50 + 1.51 + let controller = Cc["@mozilla.org/autocomplete/controller;1"]. 1.52 + getService(Ci.nsIAutoCompleteController); 1.53 + 1.54 + let input = new AutoCompleteInputBase([delayedSearch.name, 1.55 + immediateSearch.name]); 1.56 + input.completeDefaultIndex = true; 1.57 + input.textValue = inputStr; 1.58 + 1.59 + // Caret must be at the end. Autofill doesn't happen unless you're typing 1.60 + // characters at the end. 1.61 + let strLen = inputStr.length; 1.62 + input.selectTextRange(strLen, strLen); 1.63 + 1.64 + controller.input = input; 1.65 + controller.startSearch(inputStr); 1.66 + 1.67 + // Immediately check the result, the immediate search should have finished. 1.68 + do_check_eq(input.textValue, "moz-immediate"); 1.69 + 1.70 + // Wait for both queries to finish. 1.71 + input.onSearchComplete = function() { 1.72 + // Sanity check. 1.73 + do_check_eq(input.textValue, "moz-immediate"); 1.74 + 1.75 + unregisterAutoCompleteSearch(immediateSearch); 1.76 + unregisterAutoCompleteSearch(delayedSearch); 1.77 + run_next_test(); 1.78 + }; 1.79 +}); 1.80 + 1.81 +/** 1.82 + * An immediate search should be executed before any delayed search. 1.83 + */ 1.84 +add_test(function test_immediate_search_notimeout() { 1.85 + let immediateResults = ["mozillaTest"]; 1.86 + let inputStr = "moz"; 1.87 + 1.88 + let immediateSearch = new AutoCompleteImmediateSearch( 1.89 + "immediate", new AutoCompleteResult(["moz-immediate"], 0)); 1.90 + registerAutoCompleteSearch(immediateSearch); 1.91 + 1.92 + let delayedSearch = new AutoCompleteDelayedSearch( 1.93 + "delayed", new AutoCompleteResult(["moz-delayed"], 0)); 1.94 + registerAutoCompleteSearch(delayedSearch); 1.95 + 1.96 + let controller = Cc["@mozilla.org/autocomplete/controller;1"]. 1.97 + getService(Ci.nsIAutoCompleteController); 1.98 + 1.99 + let input = new AutoCompleteInputBase([delayedSearch.name, 1.100 + immediateSearch.name]); 1.101 + input.completeDefaultIndex = true; 1.102 + input.textValue = inputStr; 1.103 + input.timeout = 0; 1.104 + 1.105 + // Caret must be at the end. Autofill doesn't happen unless you're typing 1.106 + // characters at the end. 1.107 + let strLen = inputStr.length; 1.108 + input.selectTextRange(strLen, strLen); 1.109 + 1.110 + controller.input = input; 1.111 + let complete = false; 1.112 + input.onSearchComplete = function() { 1.113 + complete = true; 1.114 + }; 1.115 + controller.startSearch(inputStr); 1.116 + do_check_true(complete); 1.117 + 1.118 + // Immediately check the result, the immediate search should have finished. 1.119 + do_check_eq(input.textValue, "moz-immediate"); 1.120 + 1.121 + unregisterAutoCompleteSearch(immediateSearch); 1.122 + unregisterAutoCompleteSearch(delayedSearch); 1.123 + run_next_test(); 1.124 +}); 1.125 + 1.126 +/** 1.127 + * A delayed search should be executed synchronously with a zero timeout. 1.128 + */ 1.129 +add_test(function test_delayed_search_notimeout() { 1.130 + let immediateResults = ["mozillaTest"]; 1.131 + let inputStr = "moz"; 1.132 + 1.133 + let delayedSearch = new AutoCompleteDelayedSearch( 1.134 + "delayed", new AutoCompleteResult(["moz-delayed"], 0)); 1.135 + registerAutoCompleteSearch(delayedSearch); 1.136 + 1.137 + let controller = Cc["@mozilla.org/autocomplete/controller;1"]. 1.138 + getService(Ci.nsIAutoCompleteController); 1.139 + 1.140 + let input = new AutoCompleteInputBase([delayedSearch.name]); 1.141 + input.completeDefaultIndex = true; 1.142 + input.textValue = inputStr; 1.143 + input.timeout = 0; 1.144 + 1.145 + // Caret must be at the end. Autofill doesn't happen unless you're typing 1.146 + // characters at the end. 1.147 + let strLen = inputStr.length; 1.148 + input.selectTextRange(strLen, strLen); 1.149 + 1.150 + controller.input = input; 1.151 + let complete = false; 1.152 + input.onSearchComplete = function() { 1.153 + complete = true; 1.154 + }; 1.155 + controller.startSearch(inputStr); 1.156 + do_check_true(complete); 1.157 + 1.158 + // Immediately check the result, the delayed search should have finished. 1.159 + do_check_eq(input.textValue, "moz-delayed"); 1.160 + 1.161 + unregisterAutoCompleteSearch(delayedSearch); 1.162 + run_next_test(); 1.163 +});