gecko-dev/browser/components/sessionstore/test/content-forms.js
Rob Wu 4a6f84f91d Bug 1544834 - Replace deprecated generics in test code r=evilpie
- `Array.map` becomes `Array.from`
- Array copying via `Array.slice` becomes `Array.from`.
- `Array.forEach` that did not rely on closures becomes `for`-`of` loops.
- Anything else: `Array.X` becomes `Array.prototype.X`.

Complex cases:

dom/bindings/test/TestInterfaceJS.js and
dom/bindings/test/test_exception_options_from_jsimplemented.html
use `Array.indexOf` to generate an error with a specific error message.
Switched to `Array.prototype.forEach` to generate the same error.

js/src/jit-test/tests/basic/exception-column-number.js
In this test `Array.indexOf()` is used to generate an error. Since the
exact message doesn't matter, I switched to `Array.from()`.

Intentionally not changed:

editor/libeditor/tests/browserscope/lib/richtext/richtext/js/range.js
Did not modify because this is 3rd-party code and the code uses
feature detection as a fall back when Array generics are not used.

testing/talos/talos/tests/dromaeo/lib/mootools.js
Did not modify because mootools adds the `Array.slice` method to the
`Array` object.

Not changed because they check the implementation of Array generics:
js/src/jit-test/tests/basic/arrayNatives.js
js/src/jit-test/tests/basic/bug563243.js
js/src/jit-test/tests/basic/bug618853.js
js/src/jit-test/tests/basic/bug830967.js
js/src/jit-test/tests/jaeger/recompile/bug656753.js
js/src/jit-test/tests/self-hosting/alternate-static-and-instance-array-extras.js
js/src/tests/non262/Array/generics.js
js/src/tests/non262/Array/regress-415540.js
js/src/tests/non262/extensions/regress-355497.js
js/src/tests/non262/extensions/typedarray-set-neutering.js

Depends on D27802

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

--HG--
extra : moz-landing-system : lando
2019-04-17 19:03:19 +00:00

116 lines
3.2 KiB
JavaScript

/* 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/. */
/* eslint-env mozilla/frame-script */
"use strict";
/**
* This frame script is only loaded for sessionstore mochitests. It contains
* a bunch of utility functions used to test form data collection and
* restoration in remote browsers.
*/
function queryElement(data) {
let frame = content;
if (data.hasOwnProperty("frame")) {
frame = content.frames[data.frame];
}
let doc = frame.document;
if (data.hasOwnProperty("id")) {
return doc.getElementById(data.id);
}
if (data.hasOwnProperty("selector")) {
return doc.querySelector(data.selector);
}
if (data.hasOwnProperty("xpath")) {
let xptype = doc.defaultView.XPathResult.FIRST_ORDERED_NODE_TYPE;
return doc.evaluate(data.xpath, doc, null, xptype, null).singleNodeValue;
}
throw new Error("couldn't query element");
}
function dispatchUIEvent(input, type) {
let event = input.ownerDocument.createEvent("UIEvents");
event.initUIEvent(type, true, true, input.ownerGlobal, 0);
input.dispatchEvent(event);
}
function defineListener(type, cb) {
addMessageListener("ss-test:" + type, function({data}) {
sendAsyncMessage("ss-test:" + type, cb(data));
});
}
defineListener("getInnerHTML", function(data) {
return queryElement(data).innerHTML;
});
defineListener("getTextContent", function(data) {
return queryElement(data).textContent;
});
defineListener("getInputValue", function(data) {
return queryElement(data).value;
});
defineListener("setInputValue", function(data) {
let input = queryElement(data);
input.value = data.value;
dispatchUIEvent(input, "input");
});
defineListener("getInputChecked", function(data) {
return queryElement(data).checked;
});
defineListener("setInputChecked", function(data) {
let input = queryElement(data);
input.checked = data.checked;
dispatchUIEvent(input, "input");
});
defineListener("getSelectedIndex", function(data) {
return queryElement(data).selectedIndex;
});
defineListener("setSelectedIndex", function(data) {
let input = queryElement(data);
input.selectedIndex = data.index;
dispatchUIEvent(input, "input");
});
defineListener("getMultipleSelected", function(data) {
let input = queryElement(data);
return Array.from(input.options, (opt, idx) => idx)
.filter(idx => input.options[idx].selected);
});
defineListener("setMultipleSelected", function(data) {
let input = queryElement(data);
Array.prototype.forEach.call(input.options, (opt, idx) => opt.selected = data.indices.indexOf(idx) > -1);
dispatchUIEvent(input, "input");
});
defineListener("getFileNameArray", function(data) {
return queryElement(data).mozGetFileNameArray();
});
defineListener("setFileNameArray", function(data) {
let input = queryElement(data);
input.mozSetFileNameArray(data.names, data.names.length);
dispatchUIEvent(input, "input");
});
defineListener("setFormElementValues", function(data) {
for (let elem of content.document.forms[0].elements) {
elem.value = data.value;
dispatchUIEvent(elem, "input");
}
});