fune/toolkit/content/tests/chrome/file_autocomplete_with_composition.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

530 lines
19 KiB
JavaScript

// nsDoTestsForAutoCompleteWithComposition tests autocomplete with composition.
// Users must include SimpleTest.js and EventUtils.js.
function waitForCondition(condition, nextTest) {
var tries = 0;
var interval = setInterval(function() {
if (condition() || tries >= 30) {
moveOn();
}
tries++;
}, 100);
var moveOn = function() { clearInterval(interval); nextTest(); };
}
function nsDoTestsForAutoCompleteWithComposition(aDescription,
aWindow,
aTarget,
aAutoCompleteController,
aIsFunc,
aGetTargetValueFunc,
aOnFinishFunc) {
this._description = aDescription;
this._window = aWindow;
this._target = aTarget;
this._controller = aAutoCompleteController;
this._is = aIsFunc;
this._getTargetValue = aGetTargetValueFunc;
this._onFinish = aOnFinishFunc;
this._target.focus();
this._DefaultCompleteDefaultIndex =
this._controller.input.completeDefaultIndex;
this._doTests();
}
nsDoTestsForAutoCompleteWithComposition.prototype = {
_window: null,
_target: null,
_controller: null,
_DefaultCompleteDefaultIndex: false,
_description: "",
_is: null,
_getTargetValue() { return "not initialized"; },
_onFinish: null,
_doTests() {
if (++this._testingIndex == this._tests.length) {
this._controller.input.completeDefaultIndex =
this._DefaultCompleteDefaultIndex;
this._onFinish();
return;
}
var test = this._tests[this._testingIndex];
if (this._controller.input.completeDefaultIndex != test.completeDefaultIndex) {
this._controller.input.completeDefaultIndex = test.completeDefaultIndex;
}
test.execute(this._window);
waitForCondition(() => {
return this._controller.searchStatus >=
Ci.nsIAutoCompleteController.STATUS_COMPLETE_NO_MATCH;
},
this._checkResult.bind(this));
},
_checkResult() {
var test = this._tests[this._testingIndex];
this._is(this._getTargetValue(), test.value,
this._description + ", " + test.description + ": value");
this._is(this._controller.searchString, test.searchString,
this._description + ", " + test.description + ": searchString");
this._is(this._controller.input.popupOpen, test.popup,
this._description + ", " + test.description + ": popupOpen");
this._doTests();
},
_testingIndex: -1,
_tests: [
// Simple composition when popup hasn't been shown.
// The autocomplete popup should not be shown during composition, but
// after compositionend, the popup should be shown.
{ description: "compositionstart shouldn't open the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeCompositionChange(
{ "composition":
{ "string": "M",
"clauses":
[
{ "length": 1, "attr": COMPOSITION_ATTR_RAW_CLAUSE },
],
},
"caret": { "start": 1, "length": 0 },
"key": { key: "M" },
}, aWindow);
}, popup: false, value: "M", searchString: "",
},
{ description: "modifying composition string shouldn't open the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeCompositionChange(
{ "composition":
{ "string": "Mo",
"clauses":
[
{ "length": 2, "attr": COMPOSITION_ATTR_RAW_CLAUSE },
],
},
"caret": { "start": 2, "length": 0 },
"key": { key: "o" },
}, aWindow);
}, popup: false, value: "Mo", searchString: "",
},
{ description: "compositionend should open the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeComposition({ type: "compositioncommitasis", key: { key: "KEY_Enter" } }, aWindow);
}, popup: true, value: "Mo", searchString: "Mo",
},
// If composition starts when popup is shown, the compositionstart event
// should cause closing the popup.
{ description: "compositionstart should close the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeCompositionChange(
{ "composition":
{ "string": "z",
"clauses":
[
{ "length": 1, "attr": COMPOSITION_ATTR_RAW_CLAUSE },
],
},
"caret": { "start": 1, "length": 0 },
"key": { key: "z" },
}, aWindow);
}, popup: false, value: "Moz", searchString: "Mo",
},
{ description: "modifying composition string shouldn't reopen the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeCompositionChange(
{ "composition":
{ "string": "zi",
"clauses":
[
{ "length": 2, "attr": COMPOSITION_ATTR_RAW_CLAUSE },
],
},
"caret": { "start": 2, "length": 0 },
"key": { key: "i" },
}, aWindow);
}, popup: false, value: "Mozi", searchString: "Mo",
},
{ description: "compositionend should research the result and open the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeComposition({ type: "compositioncommitasis", key: { key: "KEY_Enter" } }, aWindow);
}, popup: true, value: "Mozi", searchString: "Mozi",
},
// If composition is cancelled, the value shouldn't be changed.
{ description: "compositionstart should reclose the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeCompositionChange(
{ "composition":
{ "string": "l",
"clauses":
[
{ "length": 1, "attr": COMPOSITION_ATTR_RAW_CLAUSE },
],
},
"caret": { "start": 1, "length": 0 },
"key": { key: "l" },
}, aWindow);
}, popup: false, value: "Mozil", searchString: "Mozi",
},
{ description: "modifying composition string shouldn't reopen the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeCompositionChange(
{ "composition":
{ "string": "ll",
"clauses":
[
{ "length": 2, "attr": COMPOSITION_ATTR_RAW_CLAUSE },
],
},
"caret": { "start": 2, "length": 0 },
"key": { key: "l" },
}, aWindow);
}, popup: false, value: "Mozill", searchString: "Mozi",
},
{ description: "modifying composition string to empty string shouldn't reopen the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeCompositionChange(
{ "composition":
{ "string": "",
"clauses":
[
{ "length": 0, "attr": 0 },
],
},
"caret": { "start": 0, "length": 0 },
"key": { key: "KEY_Backspace" },
}, aWindow);
}, popup: false, value: "Mozi", searchString: "Mozi",
},
{ description: "cancled compositionend should reopen the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeComposition({ type: "compositioncommit", data: "", key: { key: "KEY_Escape" } }, aWindow);
}, popup: true, value: "Mozi", searchString: "Mozi",
},
// But if composition replaces some characters and canceled, the search
// string should be the latest value.
{ description: "compositionstart with selected string should close the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeKey("VK_LEFT", { shiftKey: true }, aWindow);
synthesizeKey("VK_LEFT", { shiftKey: true }, aWindow);
synthesizeCompositionChange(
{ "composition":
{ "string": "z",
"clauses":
[
{ "length": 1, "attr": COMPOSITION_ATTR_RAW_CLAUSE },
],
},
"caret": { "start": 1, "length": 0 },
"key": { key: "z" },
}, aWindow);
}, popup: false, value: "Moz", searchString: "Mozi",
},
{ description: "modifying composition string shouldn't reopen the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeCompositionChange(
{ "composition":
{ "string": "zi",
"clauses":
[
{ "length": 2, "attr": COMPOSITION_ATTR_RAW_CLAUSE },
],
},
"caret": { "start": 2, "length": 0 },
"key": { key: "i" },
}, aWindow);
}, popup: false, value: "Mozi", searchString: "Mozi",
},
{ description: "modifying composition string to empty string shouldn't reopen the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeCompositionChange(
{ "composition":
{ "string": "",
"clauses":
[
{ "length": 0, "attr": 0 },
],
},
"caret": { "start": 0, "length": 0 },
"key": { key: "KEY_Backspace" },
}, aWindow);
}, popup: false, value: "Mo", searchString: "Mozi",
},
{ description: "canceled compositionend should search the result with the latest value",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeComposition({ type: "compositioncommitasis", key: { key: "KEY_Escape" } }, aWindow);
}, popup: true, value: "Mo", searchString: "Mo",
},
// If all characters are removed, the popup should be closed.
{ description: "the value becomes empty by backspace, the popup should be closed",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeKey("KEY_Backspace", {}, aWindow);
synthesizeKey("KEY_Backspace", {}, aWindow);
}, popup: false, value: "", searchString: "",
},
// composition which is canceled shouldn't cause opening the popup.
{ description: "compositionstart shouldn't open the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeCompositionChange(
{ "composition":
{ "string": "M",
"clauses":
[
{ "length": 1, "attr": COMPOSITION_ATTR_RAW_CLAUSE },
],
},
"caret": { "start": 1, "length": 0 },
"key": { key: "M" },
}, aWindow);
}, popup: false, value: "M", searchString: "",
},
{ description: "modifying composition string shouldn't open the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeCompositionChange(
{ "composition":
{ "string": "Mo",
"clauses":
[
{ "length": 2, "attr": COMPOSITION_ATTR_RAW_CLAUSE },
],
},
"caret": { "start": 2, "length": 0 },
"key": { key: "o" },
}, aWindow);
}, popup: false, value: "Mo", searchString: "",
},
{ description: "modifying composition string to empty string shouldn't open the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeCompositionChange(
{ "composition":
{ "string": "",
"clauses":
[
{ "length": 0, "attr": 0 },
],
},
"caret": { "start": 0, "length": 0 },
"key": { key: "KEY_Backspace" },
}, aWindow);
}, popup: false, value: "", searchString: "",
},
{ description: "canceled compositionend shouldn't open the popup if it was closed",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeComposition({ type: "compositioncommitasis", key: { key: "KEY_Escape" } }, aWindow);
}, popup: false, value: "", searchString: "",
},
// Down key should open the popup even if the editor is empty.
{ description: "DOWN key should open the popup even if the value is empty",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeKey("KEY_ArrowDown", {}, aWindow);
}, popup: true, value: "", searchString: "",
},
// If popup is open at starting composition, the popup should be reopened
// after composition anyway.
{ description: "compositionstart shouldn't open the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeCompositionChange(
{ "composition":
{ "string": "M",
"clauses":
[
{ "length": 1, "attr": COMPOSITION_ATTR_RAW_CLAUSE },
],
},
"caret": { "start": 1, "length": 0 },
"key": { key: "M" },
}, aWindow);
}, popup: false, value: "M", searchString: "",
},
{ description: "modifying composition string shouldn't open the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeCompositionChange(
{ "composition":
{ "string": "Mo",
"clauses":
[
{ "length": 2, "attr": COMPOSITION_ATTR_RAW_CLAUSE },
],
},
"caret": { "start": 2, "length": 0 },
"key": { key: "o" },
}, aWindow);
}, popup: false, value: "Mo", searchString: "",
},
{ description: "modifying composition string to empty string shouldn't open the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeCompositionChange(
{ "composition":
{ "string": "",
"clauses":
[
{ "length": 0, "attr": 0 },
],
},
"caret": { "start": 0, "length": 0 },
"key": { key: "KEY_Backspace" },
}, aWindow);
}, popup: false, value: "", searchString: "",
},
{ description: "canceled compositionend should open the popup if it was opened",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeComposition({ type: "compositioncommitasis", key: { key: "KEY_Escape" } }, aWindow);
}, popup: true, value: "", searchString: "",
},
// Type normally, and hit escape, the popup should be closed.
{ description: "ESCAPE should close the popup after typing something",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeKey("M", {}, aWindow);
synthesizeKey("o", {}, aWindow);
synthesizeKey("KEY_Escape", {}, aWindow);
}, popup: false, value: "Mo", searchString: "Mo",
},
// Even if the popup is closed, composition which is canceled should open
// the popup if the value isn't empty.
// XXX This might not be good behavior, but anyway, this is minor issue...
{ description: "compositionstart shouldn't open the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeCompositionChange(
{ "composition":
{ "string": "z",
"clauses":
[
{ "length": 1, "attr": COMPOSITION_ATTR_RAW_CLAUSE },
],
},
"caret": { "start": 1, "length": 0 },
"key": { key: "z" },
}, aWindow);
}, popup: false, value: "Moz", searchString: "Mo",
},
{ description: "modifying composition string shouldn't open the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeCompositionChange(
{ "composition":
{ "string": "zi",
"clauses":
[
{ "length": 2, "attr": COMPOSITION_ATTR_RAW_CLAUSE },
],
},
"caret": { "start": 2, "length": 0 },
"key": { key: "i" },
}, aWindow);
}, popup: false, value: "Mozi", searchString: "Mo",
},
{ description: "modifying composition string to empty string shouldn't open the popup",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeCompositionChange(
{ "composition":
{ "string": "",
"clauses":
[
{ "length": 0, "attr": 0 },
],
},
"caret": { "start": 0, "length": 0 },
"key": { key: "KEY_Backspace" },
}, aWindow);
}, popup: false, value: "Mo", searchString: "Mo",
},
{ description: "canceled compositionend shouldn't open the popup if the popup was closed",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeComposition({ type: "compositioncommitasis", key: { key: "KEY_Escape" } }, aWindow);
}, popup: true, value: "Mo", searchString: "Mo",
},
// House keeping...
{ description: "house keeping for next tests",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeKey("KEY_Backspace", {}, aWindow);
synthesizeKey("KEY_Backspace", {}, aWindow);
}, popup: false, value: "", searchString: "",
},
// Testing for nsIAutoCompleteInput.completeDefaultIndex being true.
{ description: "compositionstart shouldn't open the popup (completeDefaultIndex is true)",
completeDefaultIndex: true,
execute(aWindow) {
synthesizeCompositionChange(
{ "composition":
{ "string": "M",
"clauses":
[
{ "length": 1, "attr": COMPOSITION_ATTR_RAW_CLAUSE },
],
},
"caret": { "start": 1, "length": 0 },
"key": { key: "M" },
}, aWindow);
}, popup: false, value: "M", searchString: "",
},
{ description: "modifying composition string shouldn't open the popup (completeDefaultIndex is true)",
completeDefaultIndex: true,
execute(aWindow) {
synthesizeCompositionChange(
{ "composition":
{ "string": "Mo",
"clauses":
[
{ "length": 2, "attr": COMPOSITION_ATTR_RAW_CLAUSE },
],
},
"caret": { "start": 2, "length": 0 },
"key": { key: "o" },
}, aWindow);
}, popup: false, value: "Mo", searchString: "",
},
{ description: "compositionend should open the popup (completeDefaultIndex is true)",
completeDefaultIndex: true,
execute(aWindow) {
synthesizeComposition({ type: "compositioncommitasis", key: { key: "KEY_Enter" } }, aWindow);
}, popup: true, value: "Mozilla", searchString: "Mo",
},
// House keeping...
{ description: "house keeping for next tests",
completeDefaultIndex: false,
execute(aWindow) {
synthesizeKey("KEY_Backspace", {}, aWindow);
synthesizeKey("KEY_Backspace", {}, aWindow);
synthesizeKey("KEY_Backspace", {}, aWindow);
synthesizeKey("KEY_Backspace", {}, aWindow);
synthesizeKey("KEY_Backspace", {}, aWindow);
synthesizeKey("KEY_Backspace", {}, aWindow);
}, popup: false, value: "", searchString: "",
},
],
};