1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/content/tests/chrome/test_autocomplete3.xul Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,188 @@ 1.4 +<?xml version="1.0"?> 1.5 +<?xml-stylesheet href="chrome://global/skin" type="text/css"?> 1.6 +<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?> 1.7 + 1.8 +<window title="Autocomplete Widget Test 3" 1.9 + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> 1.10 + 1.11 + <script type="application/javascript" 1.12 + src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/> 1.13 + <script type="application/javascript" 1.14 + src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/> 1.15 + 1.16 +<textbox id="autocomplete" type="autocomplete" 1.17 + autocompletesearch="simple" 1.18 + onsearchcomplete="checkResult();"/> 1.19 + 1.20 +<script class="testbody" type="application/javascript"> 1.21 +<![CDATA[ 1.22 + 1.23 +// Set to indicate whether or not we want autoCompleteSimple to return a result 1.24 +var returnResult = true; 1.25 + 1.26 +const ACR = Components.interfaces.nsIAutoCompleteResult; 1.27 + 1.28 +// This result can't be constructed in-line, because otherwise we leak memory. 1.29 +function nsAutoCompleteSimpleResult(aString) 1.30 +{ 1.31 + this.searchString = aString; 1.32 + if (returnResult) { 1.33 + this.searchResult = ACR.RESULT_SUCCESS; 1.34 + this.matchCount = 1; 1.35 + this._param = "Result"; 1.36 + } 1.37 +} 1.38 + 1.39 +nsAutoCompleteSimpleResult.prototype = { 1.40 + _param: "", 1.41 + searchString: null, 1.42 + searchResult: ACR.RESULT_FAILURE, 1.43 + defaultIndex: 0, 1.44 + errorDescription: null, 1.45 + matchCount: 0, 1.46 + getValueAt: function() { return this._param; }, 1.47 + getCommentAt: function() { return null; }, 1.48 + getStyleAt: function() { return null; }, 1.49 + getImageAt: function() { return null; }, 1.50 + getFinalCompleteValueAt: function() { return this.getValueAt(); }, 1.51 + getLabelAt: function() { return null; }, 1.52 + removeValueAt: function() {} 1.53 +}; 1.54 + 1.55 +// A basic autocomplete implementation that either returns one result or none 1.56 +var autoCompleteSimpleID = Components.ID("0a2afbdb-f30e-47d1-9cb1-0cd160240aca"); 1.57 +var autoCompleteSimpleName = "@mozilla.org/autocomplete/search;1?name=simple" 1.58 +var autoCompleteSimple = { 1.59 + QueryInterface: function(iid) { 1.60 + if (iid.equals(Components.interfaces.nsISupports) || 1.61 + iid.equals(Components.interfaces.nsIFactory) || 1.62 + iid.equals(Components.interfaces.nsIAutoCompleteSearch)) 1.63 + return this; 1.64 + 1.65 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.66 + }, 1.67 + 1.68 + createInstance: function(outer, iid) { 1.69 + return this.QueryInterface(iid); 1.70 + }, 1.71 + 1.72 + startSearch: function(aString, aParam, aResult, aListener) { 1.73 + var result = new nsAutoCompleteSimpleResult(aString); 1.74 + aListener.onSearchResult(this, result); 1.75 + }, 1.76 + 1.77 + stopSearch: function() {} 1.78 +}; 1.79 + 1.80 +var componentManager = Components.manager 1.81 + .QueryInterface(Components.interfaces.nsIComponentRegistrar); 1.82 +componentManager.registerFactory(autoCompleteSimpleID, "Test Simple Autocomplete", 1.83 + autoCompleteSimpleName, autoCompleteSimple); 1.84 + 1.85 + 1.86 +// Test Bug 325842 - completeDefaultIndex 1.87 + 1.88 +SimpleTest.waitForExplicitFinish(); 1.89 +setTimeout(startTest, 0); 1.90 + 1.91 +var currentTest = 0; 1.92 + 1.93 +// Note the entries for these tests (key) are incremental. 1.94 +const tests = [ 1.95 + { completeDefaultIndex: "false", key: "r", result: "r", 1.96 + start: 1, end: 1 }, 1.97 + { completeDefaultIndex: "true", key: "e", result: "result", 1.98 + start: 2, end: 6 }, 1.99 + { completeDefaultIndex: "true", key: "t", result: "ret >> Result", 1.100 + start: 3, end: 13 } 1.101 +]; 1.102 + 1.103 +function startTest() { 1.104 + var autocomplete = $("autocomplete"); 1.105 + 1.106 + // These should not be set by default. 1.107 + is(autocomplete.hasAttribute("completedefaultindex"), false, 1.108 + "completedefaultindex not set by default"); 1.109 + 1.110 + autocomplete.completeDefaultIndex = "true"; 1.111 + 1.112 + is(autocomplete.getAttribute("completedefaultindex"), "true", 1.113 + "completedefaultindex attribute set correctly"); 1.114 + is(autocomplete.completeDefaultIndex, true, 1.115 + "autoFill getter returned correctly"); 1.116 + 1.117 + autocomplete.completeDefaultIndex = "false"; 1.118 + 1.119 + is(autocomplete.getAttribute("completedefaultindex"), "false", 1.120 + "completedefaultindex attribute set to false correctly"); 1.121 + is(autocomplete.completeDefaultIndex, false, 1.122 + "completeDefaultIndex getter returned false correctly"); 1.123 + 1.124 + checkNext(); 1.125 +} 1.126 + 1.127 +function checkNext() { 1.128 + var autocomplete = $("autocomplete"); 1.129 + 1.130 + autocomplete.completeDefaultIndex = tests[currentTest].completeDefaultIndex; 1.131 + autocomplete.focus(); 1.132 + 1.133 + synthesizeKey(tests[currentTest].key, {}); 1.134 +} 1.135 + 1.136 +function checkResult() { 1.137 + var autocomplete = $("autocomplete"); 1.138 + var style = window.getComputedStyle(autocomplete, ""); 1.139 + 1.140 + is(autocomplete.value, tests[currentTest].result, 1.141 + "Test " + currentTest + ": autocomplete.value should equal '" + 1.142 + tests[currentTest].result + "'"); 1.143 + 1.144 + is(autocomplete.selectionStart, tests[currentTest].start, 1.145 + "Test " + currentTest + ": autocomplete selection should start at " + 1.146 + tests[currentTest].start); 1.147 + 1.148 + is(autocomplete.selectionEnd, tests[currentTest].end, 1.149 + "Test " + currentTest + ": autocomplete selection should end at " + 1.150 + tests[currentTest].end); 1.151 + 1.152 + ++currentTest; 1.153 + 1.154 + if (currentTest < tests.length) 1.155 + setTimeout(checkNext, 0); 1.156 + else { 1.157 + // TODO (bug 494809): Autocomplete-in-the-middle should take in count RTL 1.158 + // and complete on VK_RIGHT or VK_LEFT based on that. It should also revert 1.159 + // what user has typed to far if he moves in the opposite direction. 1.160 + if (autocomplete.value.indexOf(">>") == -1) { 1.161 + // Test result if user accepts autocomplete suggestion. 1.162 + synthesizeKey("VK_RIGHT", {}); 1.163 + is(autocomplete.value, "Result", 1.164 + "Test complete: autocomplete.value should equal 'Result'"); 1.165 + is(autocomplete.selectionStart, 6, 1.166 + "Test complete: autocomplete selection should start at 6"); 1.167 + is(autocomplete.selectionEnd, 6, 1.168 + "Test complete: autocomplete selection should end at 6"); 1.169 + } 1.170 + 1.171 + setTimeout(function() { 1.172 + // Unregister the factory so that we don't get in the way of other tests 1.173 + componentManager.unregisterFactory(autoCompleteSimpleID, autoCompleteSimple); 1.174 + SimpleTest.finish(); 1.175 + }, 0); 1.176 + } 1.177 +} 1.178 + 1.179 +]]> 1.180 +</script> 1.181 + 1.182 +<body xmlns="http://www.w3.org/1999/xhtml"> 1.183 +<p id="display"> 1.184 +</p> 1.185 +<div id="content" style="display: none"> 1.186 +</div> 1.187 +<pre id="test"> 1.188 +</pre> 1.189 +</body> 1.190 + 1.191 +</window>