|
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 5" |
|
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 ontextentered="checkTextEntered();" |
|
16 ontextreverted="checkTextReverted();" |
|
17 onsearchbegin="checkSearchBegin();" |
|
18 onsearchcomplete="checkSearchCompleted();"/> |
|
19 |
|
20 <script class="testbody" type="application/javascript"> |
|
21 <![CDATA[ |
|
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 this.searchResult = ACR.RESULT_SUCCESS; |
|
30 this.matchCount = 1; |
|
31 this._param = "SUCCESS"; |
|
32 } |
|
33 |
|
34 nsAutoCompleteSimpleResult.prototype = { |
|
35 _param: "", |
|
36 searchString: null, |
|
37 searchResult: ACR.RESULT_FAILURE, |
|
38 defaultIndex: -1, |
|
39 errorDescription: null, |
|
40 matchCount: 0, |
|
41 getValueAt: function() { return this._param; }, |
|
42 getCommentAt: function() { return null; }, |
|
43 getStyleAt: function() { return null; }, |
|
44 getImageAt: function() { return null; }, |
|
45 getFinalCompleteValueAt: function() { return this.getValueAt(); }, |
|
46 getLabelAt: function() { return null; }, |
|
47 removeValueAt: function() {} |
|
48 }; |
|
49 |
|
50 // A basic autocomplete implementation that either returns one result or none |
|
51 var autoCompleteSimpleID = Components.ID("0a2afbdb-f30e-47d1-9cb1-0cd160240aca"); |
|
52 var autoCompleteSimpleName = "@mozilla.org/autocomplete/search;1?name=simple" |
|
53 var autoCompleteSimple = { |
|
54 QueryInterface: function(iid) { |
|
55 if (iid.equals(Components.interfaces.nsISupports) || |
|
56 iid.equals(Components.interfaces.nsIFactory) || |
|
57 iid.equals(Components.interfaces.nsIAutoCompleteSearch)) |
|
58 return this; |
|
59 |
|
60 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
61 }, |
|
62 |
|
63 createInstance: function(outer, iid) { |
|
64 return this.QueryInterface(iid); |
|
65 }, |
|
66 |
|
67 startSearch: function(aString, aParam, aResult, aListener) { |
|
68 var result = new nsAutoCompleteSimpleResult(aString); |
|
69 aListener.onSearchResult(this, result); |
|
70 }, |
|
71 |
|
72 stopSearch: function() {} |
|
73 }; |
|
74 |
|
75 var componentManager = Components.manager |
|
76 .QueryInterface(Components.interfaces.nsIComponentRegistrar); |
|
77 componentManager.registerFactory(autoCompleteSimpleID, "Test Simple Autocomplete", |
|
78 autoCompleteSimpleName, autoCompleteSimple); |
|
79 |
|
80 SimpleTest.waitForExplicitFinish(); |
|
81 setTimeout(startTest, 0); |
|
82 |
|
83 function startTest() { |
|
84 let autocomplete = $("autocomplete"); |
|
85 |
|
86 // blur the field to ensure that the popup is closed and that the previous |
|
87 // search has stopped, then start a new search. |
|
88 autocomplete.blur(); |
|
89 autocomplete.focus(); |
|
90 synthesizeKey("r", {}); |
|
91 } |
|
92 |
|
93 let hasTextEntered = false; |
|
94 let hasSearchBegun = false; |
|
95 |
|
96 function checkSearchBegin() { |
|
97 hasSearchBegun = true; |
|
98 } |
|
99 |
|
100 let test = 0; |
|
101 function checkSearchCompleted() { |
|
102 is(hasSearchBegun, true, "onsearchbegin handler has been correctly called."); |
|
103 |
|
104 if (test == 0) { |
|
105 hasSearchBegun = false; |
|
106 synthesizeKey("VK_RETURN", { }); |
|
107 } else if (test == 1) { |
|
108 hasSearchBegun = false; |
|
109 synthesizeKey("VK_ESCAPE", { }); |
|
110 } else { |
|
111 throw "checkSearchCompleted should only be called twice."; |
|
112 } |
|
113 } |
|
114 |
|
115 function checkTextEntered() { |
|
116 is(test, 0, "checkTextEntered should be reached from first test."); |
|
117 is(hasSearchBegun, false, "onsearchbegin handler should not be called on text revert."); |
|
118 |
|
119 // fire second test |
|
120 test++; |
|
121 |
|
122 let autocomplete = $("autocomplete"); |
|
123 autocomplete.textValue = ""; |
|
124 autocomplete.blur(); |
|
125 autocomplete.focus(); |
|
126 synthesizeKey("r", {}); |
|
127 } |
|
128 |
|
129 function checkTextReverted() { |
|
130 is(test, 1, "checkTextReverted should be the second test reached."); |
|
131 is(hasSearchBegun, false, "onsearchbegin handler should not be called on text revert."); |
|
132 |
|
133 setTimeout(function() { |
|
134 // Unregister the factory so that we don't get in the way of other tests |
|
135 componentManager.unregisterFactory(autoCompleteSimpleID, autoCompleteSimple); |
|
136 SimpleTest.finish(); |
|
137 }, 0); |
|
138 } |
|
139 |
|
140 ]]> |
|
141 </script> |
|
142 |
|
143 <body xmlns="http://www.w3.org/1999/xhtml"> |
|
144 <p id="display"> |
|
145 </p> |
|
146 <div id="content" style="display: none"> |
|
147 </div> |
|
148 <pre id="test"> |
|
149 </pre> |
|
150 </body> |
|
151 |
|
152 </window> |