summaryrefslogtreecommitdiffstats
path: root/toolkit/components/autocomplete/tests/unit/test_hiddenResult.js
blob: 8e248571659669c2edeb9cab73468969cf46eafb (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

function AutoCompleteResult(aValues) {
  this._values = aValues;
  this.defaultIndex = -1;
  this._typeAheadResult = false;
}
AutoCompleteResult.prototype = Object.create(AutoCompleteResultBase.prototype);

function AutoCompleteTypeAheadResult(aValues) {
  this._values = aValues;
  this.defaultIndex = 0;
  this._typeAheadResult = true;
}
AutoCompleteTypeAheadResult.prototype = Object.create(AutoCompleteResultBase.prototype);


/**
 * Test AutoComplete with multiple AutoCompleteSearch sources, with one of them
 * being hidden from the popup, but can still do typeahead completion.
 */
function run_test() {
  do_test_pending();

  var inputStr = "moz";

  // Type ahead result
  var searchTypeAhead = new AutoCompleteSearchBase("search1",
                                                   new AutoCompleteTypeAheadResult(["mozillaTest1"]));
  // Regular result
  var searchNormal = new AutoCompleteSearchBase("search2",
                                                new AutoCompleteResult(["mozillaTest2"]));

  // Register searches so AutoCompleteController can find them
  registerAutoCompleteSearch(searchNormal);
  registerAutoCompleteSearch(searchTypeAhead);

  // Make an AutoCompleteInput that uses our searches
  // and confirms results on search complete.
  var input = new AutoCompleteInputBase([searchTypeAhead.name, searchNormal.name]);
  input.completeDefaultIndex = true;
  input.textValue = inputStr;

  // Caret must be at the end. Autofill doesn't happen unless you're typing
  // characters at the end.
  var strLen = inputStr.length;
  input.selectTextRange(strLen, strLen);
  do_check_eq(input.selectionStart, strLen);
  do_check_eq(input.selectionEnd, strLen);

  var controller = Cc["@mozilla.org/autocomplete/controller;1"].
                   getService(Ci.nsIAutoCompleteController);

  controller.input = input;
  controller.startSearch(inputStr);

  input.onSearchComplete = function() {
    // Hidden results should still be able to do inline autocomplete
    do_check_eq(input.textValue, "mozillaTest1");

    // Now, let's fill the textbox with the first result of the popup.
    // The first search is marked as hidden, so we must always get the
    // second search.
    controller.handleEnter(true);
    do_check_eq(input.textValue, "mozillaTest2");

    // Only one item in the popup.
    do_check_eq(controller.matchCount, 1);

    // Unregister searches
    unregisterAutoCompleteSearch(searchNormal);
    unregisterAutoCompleteSearch(searchTypeAhead);
    do_test_finished();
  };
}