gecko-dev/toolkit/modules/tests/xpcshell/test_FinderIterator.js
Kris Maglione e930b89c34 Bug 1514594: Part 3 - Change ChromeUtils.import API.
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8

This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:

  ChromeUtils.import("resource://gre/modules/Services.jsm");

is approximately the same as the following, in the new model:

  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs

This was done using the followng script:

https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16750

--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 10:18:31 -08:00

263 lines
7.6 KiB
JavaScript

const { FinderIterator } = ChromeUtils.import("resource://gre/modules/FinderIterator.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(async function test_start() {
let findText = "test";
let rangeCount = 300;
prepareIterator(findText, rangeCount);
let count = 0;
await 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(async function test_valid_arguments() {
let findText = "foo";
let rangeCount = 20;
prepareIterator(findText, rangeCount);
let count = 0;
await 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(async 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();
await whenDone;
Assert.equal(count, 0, "Number of ranges should be 0");
FinderIterator.reset();
});
add_task(async 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");
await whenDone;
Assert.equal(count, 0, "Number of ranges should match 0");
});
add_task(async 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,
});
await 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.
await new Promise(resolve => gMockWindow.setTimeout(resolve, 10));
FinderIterator.stop();
Assert.ok(!FinderIterator.running, "Stop means stop");
await whenDone;
await 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(async 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",
});
await 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();
});