1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/content/tests/chrome/test_autocomplete2.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 2" 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 = false; 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 = "SUCCESS"; 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: -1, 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 441530 - correctly setting "nomatch" 1.87 +// Test Bug 441526 - correctly setting style with "highlightnonmatches" 1.88 + 1.89 +SimpleTest.waitForExplicitFinish(); 1.90 +setTimeout(startTest, 0); 1.91 + 1.92 +function startTest() { 1.93 + var autocomplete = $("autocomplete"); 1.94 + 1.95 + // Ensure highlightNonMatches can be set correctly. 1.96 + 1.97 + // This should not be set by default. 1.98 + is(autocomplete.hasAttribute("highlightnonmatches"), false, 1.99 + "highlight nonmatches not set by default"); 1.100 + 1.101 + autocomplete.highlightNonMatches = "true"; 1.102 + 1.103 + is(autocomplete.getAttribute("highlightnonmatches"), "true", 1.104 + "highlight non matches attribute set correctly"); 1.105 + is(autocomplete.highlightNonMatches, true, 1.106 + "highlight non matches getter returned correctly"); 1.107 + 1.108 + autocomplete.highlightNonMatches = "false"; 1.109 + 1.110 + is(autocomplete.getAttribute("highlightnonmatches"), "false", 1.111 + "highlight non matches attribute set to false correctly"); 1.112 + is(autocomplete.highlightNonMatches, false, 1.113 + "highlight non matches getter returned false correctly"); 1.114 + 1.115 + check(); 1.116 +} 1.117 + 1.118 +function check() { 1.119 + var autocomplete = $("autocomplete"); 1.120 + 1.121 + // Toggle this value, so we can re-use the one function. 1.122 + returnResult = !returnResult; 1.123 + 1.124 + // blur the field to ensure that the popup is closed and that the previous 1.125 + // search has stopped, then start a new search. 1.126 + autocomplete.blur(); 1.127 + autocomplete.focus(); 1.128 + synthesizeKey("r", {}); 1.129 +} 1.130 + 1.131 +function checkResult() { 1.132 + var autocomplete = $("autocomplete"); 1.133 + var style = window.getComputedStyle(autocomplete, ""); 1.134 + 1.135 + if (returnResult) { 1.136 + // Result was returned, so there should not be a nomatch attribute 1.137 + is(autocomplete.hasAttribute("nomatch"), false, 1.138 + "nomatch attribute shouldn't be present here"); 1.139 + 1.140 + // Ensure that the style is set correctly whichever way highlightNonMatches 1.141 + // is set. 1.142 + autocomplete.highlightNonMatches = "true"; 1.143 + 1.144 + isnot(style.getPropertyCSSValue("color").cssText, "rgb(255, 0, 0)", 1.145 + "not nomatch and highlightNonMatches - should not be red"); 1.146 + 1.147 + autocomplete.highlightNonMatches = "false"; 1.148 + 1.149 + isnot(style.getPropertyCSSValue("color").cssText, "rgb(255, 0, 0)", 1.150 + "not nomatch and not highlightNonMatches - should not be red"); 1.151 + 1.152 + setTimeout(check, 0); 1.153 + } 1.154 + else { 1.155 + // No result was returned, so there should be nomatch attribute 1.156 + is(autocomplete.getAttribute("nomatch"), "true", 1.157 + "nomatch attribute not correctly set when expected"); 1.158 + 1.159 + // Ensure that the style is set correctly whichever way highlightNonMatches 1.160 + // is set. 1.161 + autocomplete.highlightNonMatches = "true"; 1.162 + 1.163 + is(style.getPropertyCSSValue("color").cssText, "rgb(255, 0, 0)", 1.164 + "nomatch and highlightNonMatches - should be red"); 1.165 + 1.166 + autocomplete.highlightNonMatches = "false"; 1.167 + 1.168 + isnot(style.getPropertyCSSValue("color").cssText, "rgb(255, 0, 0)", 1.169 + "nomatch and not highlightNonMatches - should not be red"); 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>