1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/autocomplete/tests/unit/test_hiddenResult.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,76 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +function AutoCompleteResult(aValues) { 1.8 + this._values = aValues; 1.9 + this.defaultIndex = -1; 1.10 + this._typeAheadResult = false; 1.11 +} 1.12 +AutoCompleteResult.prototype = Object.create(AutoCompleteResultBase.prototype); 1.13 + 1.14 +function AutoCompleteTypeAheadResult(aValues) { 1.15 + this._values = aValues; 1.16 + this.defaultIndex = 0; 1.17 + this._typeAheadResult = true; 1.18 +} 1.19 +AutoCompleteTypeAheadResult.prototype = Object.create(AutoCompleteResultBase.prototype); 1.20 + 1.21 + 1.22 +/** 1.23 + * Test AutoComplete with multiple AutoCompleteSearch sources, with one of them 1.24 + * being hidden from the popup, but can still do typeahead completion. 1.25 + */ 1.26 +function run_test() { 1.27 + do_test_pending(); 1.28 + 1.29 + var inputStr = "moz"; 1.30 + 1.31 + // Type ahead result 1.32 + var searchTypeAhead = new AutoCompleteSearchBase("search1", 1.33 + new AutoCompleteTypeAheadResult(["mozillaTest1"])); 1.34 + // Regular result 1.35 + var searchNormal = new AutoCompleteSearchBase("search2", 1.36 + new AutoCompleteResult(["mozillaTest2"])); 1.37 + 1.38 + // Register searches so AutoCompleteController can find them 1.39 + registerAutoCompleteSearch(searchNormal); 1.40 + registerAutoCompleteSearch(searchTypeAhead); 1.41 + 1.42 + // Make an AutoCompleteInput that uses our searches 1.43 + // and confirms results on search complete. 1.44 + var input = new AutoCompleteInputBase([searchTypeAhead.name, searchNormal.name]); 1.45 + input.completeDefaultIndex = true; 1.46 + input.textValue = inputStr; 1.47 + 1.48 + // Caret must be at the end. Autofill doesn't happen unless you're typing 1.49 + // characters at the end. 1.50 + var strLen = inputStr.length; 1.51 + input.selectTextRange(strLen, strLen); 1.52 + do_check_eq(input.selectionStart, strLen); 1.53 + do_check_eq(input.selectionEnd, strLen); 1.54 + 1.55 + var controller = Cc["@mozilla.org/autocomplete/controller;1"]. 1.56 + getService(Ci.nsIAutoCompleteController); 1.57 + 1.58 + controller.input = input; 1.59 + controller.startSearch(inputStr); 1.60 + 1.61 + input.onSearchComplete = function() { 1.62 + // Hidden results should still be able to do inline autocomplete 1.63 + do_check_eq(input.textValue, "mozillaTest1"); 1.64 + 1.65 + // Now, let's fill the textbox with the first result of the popup. 1.66 + // The first search is marked as hidden, so we must always get the 1.67 + // second search. 1.68 + controller.handleEnter(true); 1.69 + do_check_eq(input.textValue, "mozillaTest2"); 1.70 + 1.71 + // Only one item in the popup. 1.72 + do_check_eq(controller.matchCount, 1); 1.73 + 1.74 + // Unregister searches 1.75 + unregisterAutoCompleteSearch(searchNormal); 1.76 + unregisterAutoCompleteSearch(searchTypeAhead); 1.77 + do_test_finished(); 1.78 + }; 1.79 +}