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
|
const { interfaces: Ci, classes: Cc, utils: Cu } = Components;
const { FinderIterator } = Cu.import("resource://gre/modules/FinderIterator.jsm", {});
Cu.import("resource://gre/modules/Promise.jsm");
var gFindResults = [];
// Stub the method that instantiates nsIFind and does all the interaction with
// the docShell to be searched through.
FinderIterator._iterateDocument = function* (word, window, finder) {
for (let range of gFindResults)
yield range;
};
FinderIterator._rangeStartsInLink = fakeRange => fakeRange.startsInLink;
function FakeRange(textContent, startsInLink = false) {
this.startContainer = {};
this.startsInLink = startsInLink;
this.toString = () => textContent;
}
var gMockWindow = {
setTimeout(cb, delay) {
Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer)
.initWithCallback(cb, delay, Ci.nsITimer.TYPE_ONE_SHOT);
}
};
var gMockFinder = {
_getWindow() { return gMockWindow; }
};
function prepareIterator(findText, rangeCount) {
gFindResults = [];
for (let i = rangeCount; --i >= 0;)
gFindResults.push(new FakeRange(findText));
}
add_task(function* test_start() {
let findText = "test";
let rangeCount = 300;
prepareIterator(findText, rangeCount);
let count = 0;
yield FinderIterator.start({
caseSensitive: false,
entireWord: false,
finder: gMockFinder,
listener: {
onIteratorRangeFound(range) {
++count;
Assert.equal(range.toString(), findText, "Text content should match");
}
},
word: findText
});
Assert.equal(rangeCount, count, "Amount of ranges yielded should match!");
Assert.ok(!FinderIterator.running, "Running state should match");
Assert.equal(FinderIterator._previousRanges.length, rangeCount, "Ranges cache should match");
FinderIterator.reset();
});
add_task(function* test_valid_arguments() {
let findText = "foo";
let rangeCount = 20;
prepareIterator(findText, rangeCount);
let count = 0;
yield FinderIterator.start({
caseSensitive: false,
entireWord: false,
finder: gMockFinder,
listener: { onIteratorRangeFound(range) { ++count; } },
word: findText
});
let params = FinderIterator._previousParams;
Assert.ok(!params.linksOnly, "Default for linksOnly is false");
Assert.ok(!params.useCache, "Default for useCache is false");
Assert.equal(params.word, findText, "Words should match");
count = 0;
Assert.throws(() => FinderIterator.start({
entireWord: false,
listener: { onIteratorRangeFound(range) { ++count; } },
word: findText
}), /Missing required option 'caseSensitive'/, "Should throw when missing an argument");
FinderIterator.reset();
Assert.throws(() => FinderIterator.start({
caseSensitive: false,
listener: { onIteratorRangeFound(range) { ++count; } },
word: findText
}), /Missing required option 'entireWord'/, "Should throw when missing an argument");
FinderIterator.reset();
Assert.throws(() => FinderIterator.start({
caseSensitive: false,
entireWord: false,
listener: { onIteratorRangeFound(range) { ++count; } },
word: findText
}), /Missing required option 'finder'/, "Should throw when missing an argument");
FinderIterator.reset();
Assert.throws(() => FinderIterator.start({
caseSensitive: true,
entireWord: false,
finder: gMockFinder,
word: findText
}), /Missing valid, required option 'listener'/, "Should throw when missing an argument");
FinderIterator.reset();
Assert.throws(() => FinderIterator.start({
caseSensitive: false,
entireWord: true,
finder: gMockFinder,
listener: { onIteratorRangeFound(range) { ++count; } },
}), /Missing required option 'word'/, "Should throw when missing an argument");
FinderIterator.reset();
Assert.equal(count, 0, "No ranges should've been counted");
});
add_task(function* test_stop() {
let findText = "bar";
let rangeCount = 120;
prepareIterator(findText, rangeCount);
let count = 0;
let whenDone = FinderIterator.start({
caseSensitive: false,
entireWord: false,
finder: gMockFinder,
listener: { onIteratorRangeFound(range) { ++count; } },
word: findText
});
FinderIterator.stop();
yield whenDone;
Assert.equal(count, 0, "Number of ranges should be 0");
FinderIterator.reset();
});
add_task(function* test_reset() {
let findText = "tik";
let rangeCount = 142;
prepareIterator(findText, rangeCount);
let count = 0;
let whenDone = FinderIterator.start({
caseSensitive: false,
entireWord: false,
finder: gMockFinder,
listener: { onIteratorRangeFound(range) { ++count; } },
word: findText
});
Assert.ok(FinderIterator.running, "Yup, running we are");
Assert.equal(count, 0, "Number of ranges should match 0");
Assert.equal(FinderIterator.ranges.length, 0, "Number of ranges should match 0");
FinderIterator.reset();
Assert.ok(!FinderIterator.running, "Nope, running we are not");
Assert.equal(FinderIterator.ranges.length, 0, "No ranges after reset");
Assert.equal(FinderIterator._previousRanges.length, 0, "No ranges after reset");
yield whenDone;
Assert.equal(count, 0, "Number of ranges should match 0");
});
add_task(function* test_parallel_starts() {
let findText = "tak";
let rangeCount = 2143;
prepareIterator(findText, rangeCount);
// Start off the iterator.
let count = 0;
let whenDone = FinderIterator.start({
caseSensitive: false,
entireWord: false,
finder: gMockFinder,
listener: { onIteratorRangeFound(range) { ++count; } },
word: findText
});
yield new Promise(resolve => gMockWindow.setTimeout(resolve, 120));
Assert.ok(FinderIterator.running, "We ought to be running here");
let count2 = 0;
let whenDone2 = FinderIterator.start({
caseSensitive: false,
entireWord: false,
finder: gMockFinder,
listener: { onIteratorRangeFound(range) { ++count2; } },
word: findText
});
// Let the iterator run for a little while longer before we assert the world.
yield new Promise(resolve => gMockWindow.setTimeout(resolve, 10));
FinderIterator.stop();
Assert.ok(!FinderIterator.running, "Stop means stop");
yield whenDone;
yield whenDone2;
Assert.greater(count, FinderIterator.kIterationSizeMax, "At least one range should've been found");
Assert.less(count, rangeCount, "Not all ranges should've been found");
Assert.greater(count2, FinderIterator.kIterationSizeMax, "At least one range should've been found");
Assert.less(count2, rangeCount, "Not all ranges should've been found");
Assert.equal(count2, count, "The second start was later, but should have caught up");
FinderIterator.reset();
});
add_task(function* test_allowDistance() {
let findText = "gup";
let rangeCount = 20;
prepareIterator(findText, rangeCount);
// Start off the iterator.
let count = 0;
let whenDone = FinderIterator.start({
caseSensitive: false,
entireWord: false,
finder: gMockFinder,
listener: { onIteratorRangeFound(range) { ++count; } },
word: findText
});
let count2 = 0;
let whenDone2 = FinderIterator.start({
caseSensitive: false,
entireWord: false,
finder: gMockFinder,
listener: { onIteratorRangeFound(range) { ++count2; } },
word: "gu"
});
let count3 = 0;
let whenDone3 = FinderIterator.start({
allowDistance: 1,
caseSensitive: false,
entireWord: false,
finder: gMockFinder,
listener: { onIteratorRangeFound(range) { ++count3; } },
word: "gu"
});
yield Promise.all([whenDone, whenDone2, whenDone3]);
Assert.equal(count, rangeCount, "The first iterator invocation should yield all results");
Assert.equal(count2, 0, "The second iterator invocation should yield _no_ results");
Assert.equal(count3, rangeCount, "The first iterator invocation should yield all results");
FinderIterator.reset();
});
|