|
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 3" |
|
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 = true; |
|
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 = "Result"; |
|
33 } |
|
34 } |
|
35 |
|
36 nsAutoCompleteSimpleResult.prototype = { |
|
37 _param: "", |
|
38 searchString: null, |
|
39 searchResult: ACR.RESULT_FAILURE, |
|
40 defaultIndex: 0, |
|
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 325842 - completeDefaultIndex |
|
84 |
|
85 SimpleTest.waitForExplicitFinish(); |
|
86 setTimeout(startTest, 0); |
|
87 |
|
88 var currentTest = 0; |
|
89 |
|
90 // Note the entries for these tests (key) are incremental. |
|
91 const tests = [ |
|
92 { completeDefaultIndex: "false", key: "r", result: "r", |
|
93 start: 1, end: 1 }, |
|
94 { completeDefaultIndex: "true", key: "e", result: "result", |
|
95 start: 2, end: 6 }, |
|
96 { completeDefaultIndex: "true", key: "t", result: "ret >> Result", |
|
97 start: 3, end: 13 } |
|
98 ]; |
|
99 |
|
100 function startTest() { |
|
101 var autocomplete = $("autocomplete"); |
|
102 |
|
103 // These should not be set by default. |
|
104 is(autocomplete.hasAttribute("completedefaultindex"), false, |
|
105 "completedefaultindex not set by default"); |
|
106 |
|
107 autocomplete.completeDefaultIndex = "true"; |
|
108 |
|
109 is(autocomplete.getAttribute("completedefaultindex"), "true", |
|
110 "completedefaultindex attribute set correctly"); |
|
111 is(autocomplete.completeDefaultIndex, true, |
|
112 "autoFill getter returned correctly"); |
|
113 |
|
114 autocomplete.completeDefaultIndex = "false"; |
|
115 |
|
116 is(autocomplete.getAttribute("completedefaultindex"), "false", |
|
117 "completedefaultindex attribute set to false correctly"); |
|
118 is(autocomplete.completeDefaultIndex, false, |
|
119 "completeDefaultIndex getter returned false correctly"); |
|
120 |
|
121 checkNext(); |
|
122 } |
|
123 |
|
124 function checkNext() { |
|
125 var autocomplete = $("autocomplete"); |
|
126 |
|
127 autocomplete.completeDefaultIndex = tests[currentTest].completeDefaultIndex; |
|
128 autocomplete.focus(); |
|
129 |
|
130 synthesizeKey(tests[currentTest].key, {}); |
|
131 } |
|
132 |
|
133 function checkResult() { |
|
134 var autocomplete = $("autocomplete"); |
|
135 var style = window.getComputedStyle(autocomplete, ""); |
|
136 |
|
137 is(autocomplete.value, tests[currentTest].result, |
|
138 "Test " + currentTest + ": autocomplete.value should equal '" + |
|
139 tests[currentTest].result + "'"); |
|
140 |
|
141 is(autocomplete.selectionStart, tests[currentTest].start, |
|
142 "Test " + currentTest + ": autocomplete selection should start at " + |
|
143 tests[currentTest].start); |
|
144 |
|
145 is(autocomplete.selectionEnd, tests[currentTest].end, |
|
146 "Test " + currentTest + ": autocomplete selection should end at " + |
|
147 tests[currentTest].end); |
|
148 |
|
149 ++currentTest; |
|
150 |
|
151 if (currentTest < tests.length) |
|
152 setTimeout(checkNext, 0); |
|
153 else { |
|
154 // TODO (bug 494809): Autocomplete-in-the-middle should take in count RTL |
|
155 // and complete on VK_RIGHT or VK_LEFT based on that. It should also revert |
|
156 // what user has typed to far if he moves in the opposite direction. |
|
157 if (autocomplete.value.indexOf(">>") == -1) { |
|
158 // Test result if user accepts autocomplete suggestion. |
|
159 synthesizeKey("VK_RIGHT", {}); |
|
160 is(autocomplete.value, "Result", |
|
161 "Test complete: autocomplete.value should equal 'Result'"); |
|
162 is(autocomplete.selectionStart, 6, |
|
163 "Test complete: autocomplete selection should start at 6"); |
|
164 is(autocomplete.selectionEnd, 6, |
|
165 "Test complete: autocomplete selection should end at 6"); |
|
166 } |
|
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> |