|
1 <?xml version="1.0"?> |
|
2 <?xml-stylesheet href="chrome://global/skin" type="text/css"?> |
|
3 <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?> |
|
4 |
|
5 <window title="Autocomplete Widget Test 2" |
|
6 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> |
|
7 |
|
8 <script type="application/javascript" |
|
9 src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/> |
|
10 <script type="application/javascript" |
|
11 src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/> |
|
12 |
|
13 <textbox id="autocomplete" type="autocomplete" |
|
14 autocompletesearch="simple" |
|
15 onsearchcomplete="checkResult();"/> |
|
16 |
|
17 <script class="testbody" type="application/javascript"> |
|
18 <![CDATA[ |
|
19 |
|
20 // Set to indicate whether or not we want autoCompleteSimple to return a result |
|
21 var returnResult = false; |
|
22 |
|
23 const ACR = Components.interfaces.nsIAutoCompleteResult; |
|
24 |
|
25 // This result can't be constructed in-line, because otherwise we leak memory. |
|
26 function nsAutoCompleteSimpleResult(aString) |
|
27 { |
|
28 this.searchString = aString; |
|
29 if (returnResult) { |
|
30 this.searchResult = ACR.RESULT_SUCCESS; |
|
31 this.matchCount = 1; |
|
32 this._param = "SUCCESS"; |
|
33 } |
|
34 } |
|
35 |
|
36 nsAutoCompleteSimpleResult.prototype = { |
|
37 _param: "", |
|
38 searchString: null, |
|
39 searchResult: ACR.RESULT_FAILURE, |
|
40 defaultIndex: -1, |
|
41 errorDescription: null, |
|
42 matchCount: 0, |
|
43 getValueAt: function() { return this._param; }, |
|
44 getCommentAt: function() { return null; }, |
|
45 getStyleAt: function() { return null; }, |
|
46 getImageAt: function() { return null; }, |
|
47 getFinalCompleteValueAt: function() { return this.getValueAt(); }, |
|
48 getLabelAt: function() { return null; }, |
|
49 removeValueAt: function() {} |
|
50 }; |
|
51 |
|
52 // A basic autocomplete implementation that either returns one result or none |
|
53 var autoCompleteSimpleID = Components.ID("0a2afbdb-f30e-47d1-9cb1-0cd160240aca"); |
|
54 var autoCompleteSimpleName = "@mozilla.org/autocomplete/search;1?name=simple" |
|
55 var autoCompleteSimple = { |
|
56 QueryInterface: function(iid) { |
|
57 if (iid.equals(Components.interfaces.nsISupports) || |
|
58 iid.equals(Components.interfaces.nsIFactory) || |
|
59 iid.equals(Components.interfaces.nsIAutoCompleteSearch)) |
|
60 return this; |
|
61 |
|
62 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
63 }, |
|
64 |
|
65 createInstance: function(outer, iid) { |
|
66 return this.QueryInterface(iid); |
|
67 }, |
|
68 |
|
69 startSearch: function(aString, aParam, aResult, aListener) { |
|
70 var result = new nsAutoCompleteSimpleResult(aString); |
|
71 aListener.onSearchResult(this, result); |
|
72 }, |
|
73 |
|
74 stopSearch: function() {} |
|
75 }; |
|
76 |
|
77 var componentManager = Components.manager |
|
78 .QueryInterface(Components.interfaces.nsIComponentRegistrar); |
|
79 componentManager.registerFactory(autoCompleteSimpleID, "Test Simple Autocomplete", |
|
80 autoCompleteSimpleName, autoCompleteSimple); |
|
81 |
|
82 |
|
83 // Test Bug 441530 - correctly setting "nomatch" |
|
84 // Test Bug 441526 - correctly setting style with "highlightnonmatches" |
|
85 |
|
86 SimpleTest.waitForExplicitFinish(); |
|
87 setTimeout(startTest, 0); |
|
88 |
|
89 function startTest() { |
|
90 var autocomplete = $("autocomplete"); |
|
91 |
|
92 // Ensure highlightNonMatches can be set correctly. |
|
93 |
|
94 // This should not be set by default. |
|
95 is(autocomplete.hasAttribute("highlightnonmatches"), false, |
|
96 "highlight nonmatches not set by default"); |
|
97 |
|
98 autocomplete.highlightNonMatches = "true"; |
|
99 |
|
100 is(autocomplete.getAttribute("highlightnonmatches"), "true", |
|
101 "highlight non matches attribute set correctly"); |
|
102 is(autocomplete.highlightNonMatches, true, |
|
103 "highlight non matches getter returned correctly"); |
|
104 |
|
105 autocomplete.highlightNonMatches = "false"; |
|
106 |
|
107 is(autocomplete.getAttribute("highlightnonmatches"), "false", |
|
108 "highlight non matches attribute set to false correctly"); |
|
109 is(autocomplete.highlightNonMatches, false, |
|
110 "highlight non matches getter returned false correctly"); |
|
111 |
|
112 check(); |
|
113 } |
|
114 |
|
115 function check() { |
|
116 var autocomplete = $("autocomplete"); |
|
117 |
|
118 // Toggle this value, so we can re-use the one function. |
|
119 returnResult = !returnResult; |
|
120 |
|
121 // blur the field to ensure that the popup is closed and that the previous |
|
122 // search has stopped, then start a new search. |
|
123 autocomplete.blur(); |
|
124 autocomplete.focus(); |
|
125 synthesizeKey("r", {}); |
|
126 } |
|
127 |
|
128 function checkResult() { |
|
129 var autocomplete = $("autocomplete"); |
|
130 var style = window.getComputedStyle(autocomplete, ""); |
|
131 |
|
132 if (returnResult) { |
|
133 // Result was returned, so there should not be a nomatch attribute |
|
134 is(autocomplete.hasAttribute("nomatch"), false, |
|
135 "nomatch attribute shouldn't be present here"); |
|
136 |
|
137 // Ensure that the style is set correctly whichever way highlightNonMatches |
|
138 // is set. |
|
139 autocomplete.highlightNonMatches = "true"; |
|
140 |
|
141 isnot(style.getPropertyCSSValue("color").cssText, "rgb(255, 0, 0)", |
|
142 "not nomatch and highlightNonMatches - should not be red"); |
|
143 |
|
144 autocomplete.highlightNonMatches = "false"; |
|
145 |
|
146 isnot(style.getPropertyCSSValue("color").cssText, "rgb(255, 0, 0)", |
|
147 "not nomatch and not highlightNonMatches - should not be red"); |
|
148 |
|
149 setTimeout(check, 0); |
|
150 } |
|
151 else { |
|
152 // No result was returned, so there should be nomatch attribute |
|
153 is(autocomplete.getAttribute("nomatch"), "true", |
|
154 "nomatch attribute not correctly set when expected"); |
|
155 |
|
156 // Ensure that the style is set correctly whichever way highlightNonMatches |
|
157 // is set. |
|
158 autocomplete.highlightNonMatches = "true"; |
|
159 |
|
160 is(style.getPropertyCSSValue("color").cssText, "rgb(255, 0, 0)", |
|
161 "nomatch and highlightNonMatches - should be red"); |
|
162 |
|
163 autocomplete.highlightNonMatches = "false"; |
|
164 |
|
165 isnot(style.getPropertyCSSValue("color").cssText, "rgb(255, 0, 0)", |
|
166 "nomatch and not highlightNonMatches - should not be red"); |
|
167 |
|
168 setTimeout(function() { |
|
169 // Unregister the factory so that we don't get in the way of other tests |
|
170 componentManager.unregisterFactory(autoCompleteSimpleID, autoCompleteSimple); |
|
171 SimpleTest.finish(); |
|
172 }, 0); |
|
173 } |
|
174 } |
|
175 |
|
176 ]]> |
|
177 </script> |
|
178 |
|
179 <body xmlns="http://www.w3.org/1999/xhtml"> |
|
180 <p id="display"> |
|
181 </p> |
|
182 <div id="content" style="display: none"> |
|
183 </div> |
|
184 <pre id="test"> |
|
185 </pre> |
|
186 </body> |
|
187 |
|
188 </window> |