/* 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/. */
const { element, ShadowRoot, WebElement, WebFrame, WebReference, WebWindow } =
ChromeUtils.importESModule(
"chrome://remote/content/marionette/element.sys.mjs"
);
const { NodeCache } = ChromeUtils.importESModule(
"chrome://remote/content/shared/webdriver/NodeCache.sys.mjs"
);
const MemoryReporter = Cc["@mozilla.org/memory-reporter-manager;1"].getService(
Ci.nsIMemoryReporterManager
);
class MockElement {
constructor(tagName, attrs = {}) {
this.tagName = tagName;
this.localName = tagName;
this.isConnected = false;
this.ownerGlobal = {
document: {
isActive() {
return true;
},
},
};
for (let attr in attrs) {
this[attr] = attrs[attr];
}
}
get nodeType() {
return 1;
}
get ELEMENT_NODE() {
return 1;
}
// this is a severely limited CSS selector
// that only supports lists of tag names
matches(selector) {
let tags = selector.split(",");
return tags.includes(this.localName);
}
}
class MockXULElement extends MockElement {
constructor(tagName, attrs = {}) {
super(tagName, attrs);
this.namespaceURI = XUL_NS;
if (typeof this.ownerDocument == "undefined") {
this.ownerDocument = {};
}
if (typeof this.ownerDocument.documentElement == "undefined") {
this.ownerDocument.documentElement = { namespaceURI: XUL_NS };
}
}
}
const xulEl = new MockXULElement("text");
const domElInPrivilegedDocument = new MockElement("input", {
nodePrincipal: { isSystemPrincipal: true },
});
const xulElInPrivilegedDocument = new MockXULElement("text", {
nodePrincipal: { isSystemPrincipal: true },
});
function setupTest() {
const browser = Services.appShell.createWindowlessBrowser(false);
browser.document.body.innerHTML = `
`;
const divEl = browser.document.querySelector("div");
const svgEl = browser.document.querySelector("svg");
const textareaEl = browser.document.querySelector("textarea");
const videoEl = browser.document.querySelector("video");
const iframeEl = browser.document.querySelector("iframe");
const childEl = iframeEl.contentDocument.createElement("div");
iframeEl.contentDocument.body.appendChild(childEl);
const shadowRoot = videoEl.openOrClosedShadowRoot;
return {
browser,
nodeCache: new NodeCache(),
childEl,
divEl,
iframeEl,
shadowRoot,
svgEl,
textareaEl,
videoEl,
};
}
add_task(function test_findClosest() {
const { divEl, videoEl } = setupTest();
equal(element.findClosest(divEl, "foo"), null);
equal(element.findClosest(videoEl, "div"), divEl);
});
add_task(function test_isSelected() {
const { browser, divEl } = setupTest();
const checkbox = browser.document.createElement("input");
checkbox.setAttribute("type", "checkbox");
ok(!element.isSelected(checkbox));
checkbox.checked = true;
ok(element.isSelected(checkbox));
// selected is not a property of
checkbox.selected = true;
checkbox.checked = false;
ok(!element.isSelected(checkbox));
const option = browser.document.createElement("option");
ok(!element.isSelected(option));
option.selected = true;
ok(element.isSelected(option));
// checked is not a property of