summaryrefslogtreecommitdiffstats
path: root/toolkit/components/autocomplete/tests/unit/test_immediate_search.js
blob: a4524ca5c997679f320fdedc655cedc0cff7d37a (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

function AutoCompleteImmediateSearch(aName, aResult) {
  this.name = aName;
  this._result = aResult;
}
AutoCompleteImmediateSearch.prototype = Object.create(
  AutoCompleteSearchBase.prototype
);
AutoCompleteImmediateSearch.prototype.searchType =
  Ci.nsIAutoCompleteSearchDescriptor.SEARCH_TYPE_IMMEDIATE;
AutoCompleteImmediateSearch.prototype.QueryInterface = ChromeUtils.generateQI([
  "nsIFactory",
  "nsIAutoCompleteSearch",
  "nsIAutoCompleteSearchDescriptor",
]);

function AutoCompleteDelayedSearch(aName, aResult) {
  this.name = aName;
  this._result = aResult;
}
AutoCompleteDelayedSearch.prototype = Object.create(
  AutoCompleteSearchBase.prototype
);

function AutoCompleteResult(aValues, aDefaultIndex) {
  this._values = aValues;
  this.defaultIndex = aDefaultIndex;
}
AutoCompleteResult.prototype = Object.create(AutoCompleteResultBase.prototype);

/**
 * An immediate search should be executed synchronously.
 */
add_test(function test_immediate_search() {
  let inputStr = "moz";

  let immediateSearch = new AutoCompleteImmediateSearch(
    "immediate",
    new AutoCompleteResult(["moz-immediate"], 0)
  );
  registerAutoCompleteSearch(immediateSearch);
  let delayedSearch = new AutoCompleteDelayedSearch(
    "delayed",
    new AutoCompleteResult(["moz-delayed"], 0)
  );
  registerAutoCompleteSearch(delayedSearch);

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

  let input = new AutoCompleteInputBase([
    delayedSearch.name,
    immediateSearch.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.
  let strLen = inputStr.length;
  input.selectTextRange(strLen, strLen);

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

  // Immediately check the result, the immediate search should have finished.
  Assert.equal(input.textValue, "moz-immediate");

  // Wait for both queries to finish.
  input.onSearchComplete = function () {
    // Sanity check.
    Assert.equal(input.textValue, "moz-immediate");

    unregisterAutoCompleteSearch(immediateSearch);
    unregisterAutoCompleteSearch(delayedSearch);
    run_next_test();
  };
});

/**
 * An immediate search should be executed before any delayed search.
 */
add_test(function test_immediate_search_notimeout() {
  let inputStr = "moz";

  let immediateSearch = new AutoCompleteImmediateSearch(
    "immediate",
    new AutoCompleteResult(["moz-immediate"], 0)
  );
  registerAutoCompleteSearch(immediateSearch);

  let delayedSearch = new AutoCompleteDelayedSearch(
    "delayed",
    new AutoCompleteResult(["moz-delayed"], 0)
  );
  registerAutoCompleteSearch(delayedSearch);

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

  let input = new AutoCompleteInputBase([
    delayedSearch.name,
    immediateSearch.name,
  ]);
  input.completeDefaultIndex = true;
  input.textValue = inputStr;
  input.timeout = 0;

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

  controller.input = input;
  let complete = false;
  input.onSearchComplete = function () {
    complete = true;
  };
  controller.startSearch(inputStr);
  Assert.ok(complete);

  // Immediately check the result, the immediate search should have finished.
  Assert.equal(input.textValue, "moz-immediate");

  unregisterAutoCompleteSearch(immediateSearch);
  unregisterAutoCompleteSearch(delayedSearch);
  run_next_test();
});

/**
 * A delayed search should be executed synchronously with a zero timeout.
 */
add_test(function test_delayed_search_notimeout() {
  let inputStr = "moz";

  let delayedSearch = new AutoCompleteDelayedSearch(
    "delayed",
    new AutoCompleteResult(["moz-delayed"], 0)
  );
  registerAutoCompleteSearch(delayedSearch);

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

  let input = new AutoCompleteInputBase([delayedSearch.name]);
  input.completeDefaultIndex = true;
  input.textValue = inputStr;
  input.timeout = 0;

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

  controller.input = input;
  let complete = false;
  input.onSearchComplete = function () {
    complete = true;
  };
  controller.startSearch(inputStr);
  Assert.ok(complete);

  // Immediately check the result, the delayed search should have finished.
  Assert.equal(input.textValue, "moz-delayed");

  unregisterAutoCompleteSearch(delayedSearch);
  run_next_test();
});