summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/chrome/test_autocomplete4.xhtml
blob: bb16194e554ef88a28799f3873c3e2fd83c7b9a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>

<window title="Autocomplete Widget Test 4"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        xmlns:html="http://www.w3.org/1999/xhtml">

  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>

<html:input id="autocomplete"
            is="autocomplete-input"
            completedefaultindex="true"
            autocompletesearch="simple"/>

<script class="testbody" type="application/javascript">
<![CDATA[

// Set to indicate whether or not we want autoCompleteSimple to return a result
var returnResult = true;

const IS_MAC = navigator.platform.includes("Mac");

const ACR = Ci.nsIAutoCompleteResult;

// This result can't be constructed in-line, because otherwise we leak memory.
function nsAutoCompleteSimpleResult(aString)
{
  this.searchString = aString;
  if (returnResult) {
    this.searchResult = ACR.RESULT_SUCCESS;
    this.matchCount = 1;
    this._param = "Result";
  }
}

nsAutoCompleteSimpleResult.prototype = {
 _param: "",
 searchString: null,
 searchResult: ACR.RESULT_FAILURE,
 defaultIndex: 0,
 errorDescription: null,
 matchCount: 0,
 getValueAt() { return this._param; },
 getCommentAt() { return null; },
 getStyleAt() { return null; },
 getImageAt() { return null; },
 getFinalCompleteValueAt() { return this.getValueAt(); },
 getLabelAt() { return null; },
 removeValueAt() {}
};

// A basic autocomplete implementation that either returns one result or none
var autoCompleteSimpleID = Components.ID("0a2afbdb-f30e-47d1-9cb1-0cd160240aca");
var autoCompleteSimpleName = "@mozilla.org/autocomplete/search;1?name=simple"
var autoCompleteSimple = {
  QueryInterface: ChromeUtils.generateQI(["nsIFactory", "nsIAutoCompleteSearch"]),

  createInstance(iid) {
    return this.QueryInterface(iid);
  },

  startSearch(aString, aParam, aResult, aListener) {
    var result = new nsAutoCompleteSimpleResult(aString);
    aListener.onSearchResult(this, result);
  },

  stopSearch() {}
};

var componentManager = Components.manager
                                 .QueryInterface(Ci.nsIComponentRegistrar);
componentManager.registerFactory(autoCompleteSimpleID, "Test Simple Autocomplete",
                                 autoCompleteSimpleName, autoCompleteSimple);

let element = document.getElementById("autocomplete");

// Create stub to intercept `onSearchComplete` event.
element.onSearchComplete = function(original) {
  return function() {
    original.apply(this, arguments);
    searchComplete();
  };
}(element.onSearchComplete);

// Test Bug 325842 - completeDefaultIndex

SimpleTest.waitForExplicitFinish();

setTimeout(nextTest, 0);

var currentTest = null;

// Note the entries for these tests (key) are incremental.
const tests = [
  {
    desc: "HOME key remove selection",
    key: "KEY_Home",
    removeSelection: true,
    result: "re",
    start: 0, end: 0
  },
  {
    desc: "LEFT key remove selection",
    key: "KEY_ArrowLeft",
    removeSelection: true,
    result: "re",
    start: 1, end: 1
  },
  { desc: "RIGHT key remove selection",
    key: "KEY_ArrowRight",
    removeSelection: true,
    result: "re",
    start: 2, end: 2
  },
  { desc: "ENTER key remove selection",
    key: "KEY_Enter",
    removeSelection: true,
    result: "re",
    start: 2, end: 2
  },
  {
    desc: "HOME key",
    key: "KEY_Home",
    removeSelection: false,
    result: "Result",
    start: 0, end: 0
  },
  {
    desc: "LEFT key",
    key: "KEY_ArrowLeft",
    removeSelection: false,
    result: "Result",
    start: 5, end: 5
  },
  { desc: "RIGHT key",
    key: "KEY_ArrowRight",
    removeSelection: false,
    result: "Result",
    start: 6, end: 6
  },
  { desc: "RETURN key",
    key: "KEY_Enter",
    removeSelection: false,
    result: "Result",
    start: 6, end: 6
  },
  { desc: "TAB key should confirm suggestion when forcecomplete is set",
    key: "KEY_Tab",
    removeSelection: false,
    forceComplete: true,
    result: "Result",
    start: 6, end: 6
  },

  { desc: "RIGHT key complete from middle",
    key: "KEY_ArrowRight",
    forceComplete: true,
    completeFromMiddle: true,
    result: "Result",
    start: 6, end: 6
  },
  {
    desc: "RIGHT key w/ minResultsForPopup=2",
    key: "KEY_ArrowRight",
    removeSelection: false,
    minResultsForPopup: 2,
    result: "Result",
    start: 6, end: 6
  },
];

function nextTest() {
  if (!tests.length) {
    // No more tests to run, finish.
    setTimeout(function() {
      // Unregister the factory so that we don't get in the way of other tests
      componentManager.unregisterFactory(autoCompleteSimpleID, autoCompleteSimple);
      SimpleTest.finish();
    }, 0);
    return;
  }

  var autocomplete = $("autocomplete");
  autocomplete.value = "";
  currentTest = tests.shift();

  // HOME key works differently on Mac, so we skip tests using it.
  if (currentTest.key == "KEY_Home" && IS_MAC)
    nextTest();
  else
    setTimeout(runCurrentTest, 0);
}

function runCurrentTest() {
  var autocomplete = $("autocomplete");
  if ("minResultsForPopup" in currentTest)
    autocomplete.setAttribute("minresultsforpopup", currentTest.minResultsForPopup)
  else
    autocomplete.removeAttribute("minresultsforpopup");

  autocomplete.focus();

  if (!currentTest.completeFromMiddle) {
    sendString("re");
  }
  else {
    sendString("lt");
  }
}

function searchComplete() {
  var autocomplete = $("autocomplete");
  autocomplete.setAttribute("forcecomplete", currentTest.forceComplete);

  if (currentTest.completeFromMiddle) {
    if (!currentTest.forceComplete) {
      synthesizeKey(currentTest.key);
    }
    else if (!/ >> /.test(autocomplete.value)) {
      // At this point we should have a value like "lt >> Result" showing.
      throw new Error("Expected an middle-completed value, got " + autocomplete.value);
    }

    // For forceComplete a blur should cause a value from the results to get
    // completed to. E.g. "lt >> Result" will turn into "Result".
    if (currentTest.forceComplete)
      autocomplete.blur();

    checkResult();
    return;
  }

  is(autocomplete.value, "result",
     "Test '" + currentTest.desc + "': autocomplete.value should equal 'result'");

  if (autocomplete.selectionStart == 2) {  // Finished inserting "re" string.
    if (currentTest.removeSelection) {
      // remove current selection
      synthesizeKey("KEY_Delete");
    }

    synthesizeKey(currentTest.key);

    checkResult();
  }
}

function checkResult() {
  var autocomplete = $("autocomplete");

  is(autocomplete.value, currentTest.result,
     "Test '" + currentTest.desc + "': autocomplete.value should equal '" +
     currentTest.result + "'");

  is(autocomplete.selectionStart, currentTest.start,
     "Test '" + currentTest.desc + "': autocomplete selection should start at " +
     currentTest.start);

  is(autocomplete.selectionEnd, currentTest.end,
     "Test '" + currentTest.desc + "': autocomplete selection should end at " +
     currentTest.end);

  setTimeout(nextTest, 0);
}

]]>
</script>

<body xmlns="http://www.w3.org/1999/xhtml">
<p id="display">
</p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
</body>

</window>