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 { NodeCache } = ChromeUtils.importESModule(
"chrome://remote/content/shared/webdriver/NodeCache.sys.mjs"
);
function setupTest() {
const browser = Services.appShell.createWindowlessBrowser(false);
browser.document.body.innerHTML = `
<div id="foo" style="margin: 50px">
<iframe></iframe>
<video></video>
<svg xmlns="http://www.w3.org/2000/svg"></svg>
<textarea></textarea>
</div>
<div id="with-comment"><!-- Comment --></div>
`;
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,
seenNodeIds: new Map(),
svgEl,
textareaEl,
videoEl,
};
}
add_task(function getOrCreateNodeReference_invalid() {
const { nodeCache, seenNodeIds } = setupTest();
const invalidValues = [null, undefined, "foo", 42, true, [], {}];
for (const value of invalidValues) {
info(`Testing value: ${value}`);
Assert.throws(
() => nodeCache.getOrCreateNodeReference(value, seenNodeIds),
/TypeError/
);
}
});
add_task(function getOrCreateNodeReference_supportedNodeTypes() {
const { browser, divEl, nodeCache, seenNodeIds } = setupTest();
// Bug 1820734: No ownerGlobal is available in XPCShell tests
// const xmlDocument = new DOMParser().parseFromString(
// "<xml></xml>",
// "application/xml"
// );
const values = [
{ node: divEl, type: Node.ELEMENT_NODE },
{ node: divEl.attributes[0], type: Node.ATTRIBUTE_NODE },
{ node: browser.document.createTextNode("foo"), type: Node.TEXT_NODE },
// Bug 1820734: No ownerGlobal is available in XPCShell tests
// {
// node: xmlDocument.createCDATASection("foo"),
// type: Node.CDATA_SECTION_NODE,
// },
{
node: browser.document.createProcessingInstruction(
"xml-stylesheet",
"href='foo.css'"
),
type: Node.PROCESSING_INSTRUCTION_NODE_NODE,
},
{ node: browser.document.createComment("foo"), type: Node.COMMENT_NODE },
{ node: browser.document, type: Node.Document_NODE },
{
node: browser.document.implementation.createDocumentType(
"foo",
"bar",
"dtd"
),
type: Node.DOCUMENT_TYPE_NODE_NODE,
},
{
node: browser.document.createDocumentFragment(),
type: Node.DOCUMENT_FRAGMENT_NODE,
},
];
values.forEach((value, index) => {
info(`Testing value: ${value.type}`);
const nodeRef = nodeCache.getOrCreateNodeReference(value.node, seenNodeIds);
equal(nodeCache.size, index + 1);
equal(typeof nodeRef, "string");
ok(seenNodeIds.get(browser.browsingContext).includes(nodeRef));
});
});
add_task(function getOrCreateNodeReference_referenceAlreadyCreated() {
const { browser, divEl, nodeCache, seenNodeIds } = setupTest();
const divElRef = nodeCache.getOrCreateNodeReference(divEl, seenNodeIds);
const divElRefOther = nodeCache.getOrCreateNodeReference(divEl, seenNodeIds);
equal(divElRefOther, divElRef);
equal(nodeCache.size, 1);
equal(seenNodeIds.size, 1);
ok(seenNodeIds.get(browser.browsingContext).includes(divElRef));
});
add_task(function getOrCreateNodeReference_differentReference() {
const { browser, divEl, nodeCache, seenNodeIds, shadowRoot } = setupTest();
const divElRef = nodeCache.getOrCreateNodeReference(divEl, seenNodeIds);
equal(nodeCache.size, 1);
equal(seenNodeIds.size, 1);
ok(seenNodeIds.get(browser.browsingContext).includes(divElRef));
const shadowRootRef = nodeCache.getOrCreateNodeReference(
shadowRoot,
seenNodeIds
);
equal(nodeCache.size, 2);
equal(seenNodeIds.size, 1);
ok(seenNodeIds.get(browser.browsingContext).includes(divElRef));
ok(seenNodeIds.get(browser.browsingContext).includes(shadowRootRef));
notEqual(divElRef, shadowRootRef);
});
add_task(function getOrCreateNodeReference_differentReferencePerNodeCache() {
const { browser, divEl, nodeCache, seenNodeIds } = setupTest();
const nodeCache2 = new NodeCache();
const divElRef1 = nodeCache.getOrCreateNodeReference(divEl, seenNodeIds);
const divElRef2 = nodeCache2.getOrCreateNodeReference(divEl, seenNodeIds);
notEqual(divElRef1, divElRef2);
equal(
nodeCache.getNode(browser.browsingContext, divElRef1),
nodeCache2.getNode(browser.browsingContext, divElRef2)
);
equal(seenNodeIds.size, 1);
ok(seenNodeIds.get(browser.browsingContext).includes(divElRef1));
ok(seenNodeIds.get(browser.browsingContext).includes(divElRef2));
equal(nodeCache.getNode(browser.browsingContext, divElRef2), null);
});
add_task(function clear() {
const { browser, divEl, nodeCache, seenNodeIds, svgEl } = setupTest();
nodeCache.getOrCreateNodeReference(divEl, seenNodeIds);
nodeCache.getOrCreateNodeReference(svgEl, seenNodeIds);
equal(nodeCache.size, 2);
equal(seenNodeIds.size, 1);
// Clear requires explicit arguments.
Assert.throws(() => nodeCache.clear(), /Error/);
// Clear references for a different browsing context
const browser2 = Services.appShell.createWindowlessBrowser(false);
const imgEl = browser2.document.createElement("img");
const imgElRef = nodeCache.getOrCreateNodeReference(imgEl, seenNodeIds);
equal(nodeCache.size, 3);
equal(seenNodeIds.size, 2);
nodeCache.clear({ browsingContext: browser.browsingContext });
equal(nodeCache.size, 1);
equal(nodeCache.getNode(browser2.browsingContext, imgElRef), imgEl);
// Clear all references
nodeCache.getOrCreateNodeReference(divEl, seenNodeIds);
equal(nodeCache.size, 2);
equal(seenNodeIds.size, 2);
nodeCache.clear({ all: true });
equal(nodeCache.size, 0);
});
add_task(function getNode_multiple_nodes() {
const { browser, divEl, nodeCache, seenNodeIds, svgEl } = setupTest();
const divElRef = nodeCache.getOrCreateNodeReference(divEl, seenNodeIds);
const svgElRef = nodeCache.getOrCreateNodeReference(svgEl, seenNodeIds);
equal(nodeCache.getNode(browser.browsingContext, svgElRef), svgEl);
equal(nodeCache.getNode(browser.browsingContext, divElRef), divEl);
});
add_task(function getNode_differentBrowsingContextInSameGroup() {
const { iframeEl, divEl, nodeCache, seenNodeIds } = setupTest();
const divElRef = nodeCache.getOrCreateNodeReference(divEl, seenNodeIds);
equal(nodeCache.size, 1);
equal(
nodeCache.getNode(iframeEl.contentWindow.browsingContext, divElRef),
divEl
);
});
add_task(function getNode_differentBrowsingContextInOtherGroup() {
const { divEl, nodeCache, seenNodeIds } = setupTest();
const divElRef = nodeCache.getOrCreateNodeReference(divEl, seenNodeIds);
equal(nodeCache.size, 1);
const browser2 = Services.appShell.createWindowlessBrowser(false);
equal(nodeCache.getNode(browser2.browsingContext, divElRef), null);
});
add_task(async function getNode_nodeDeleted() {
const { browser, nodeCache, seenNodeIds } = setupTest();
let el = browser.document.createElement("div");
const elRef = nodeCache.getOrCreateNodeReference(el, seenNodeIds);
// Delete element and force a garbage collection
el = null;
await doGC();
equal(nodeCache.getNode(browser.browsingContext, elRef), null);
});
add_task(function getNodeDetails_forTopBrowsingContext() {
const { browser, divEl, nodeCache, seenNodeIds } = setupTest();
const divElRef = nodeCache.getOrCreateNodeReference(divEl, seenNodeIds);
const nodeDetails = nodeCache.getReferenceDetails(divElRef);
equal(nodeDetails.browserId, browser.browsingContext.browserId);
equal(nodeDetails.browsingContextGroupId, browser.browsingContext.group.id);
equal(nodeDetails.browsingContextId, browser.browsingContext.id);
ok(nodeDetails.isTopBrowsingContext);
ok(nodeDetails.nodeWeakRef);
equal(nodeDetails.nodeWeakRef.get(), divEl);
});
add_task(async function getNodeDetails_forChildBrowsingContext() {
const { browser, iframeEl, childEl, nodeCache, seenNodeIds } = setupTest();
const childElRef = nodeCache.getOrCreateNodeReference(childEl, seenNodeIds);
const nodeDetails = nodeCache.getReferenceDetails(childElRef);
equal(nodeDetails.browserId, browser.browsingContext.browserId);
equal(nodeDetails.browsingContextGroupId, browser.browsingContext.group.id);
equal(
nodeDetails.browsingContextId,
iframeEl.contentWindow.browsingContext.id
);
ok(!nodeDetails.isTopBrowsingContext);
ok(nodeDetails.nodeWeakRef);
equal(nodeDetails.nodeWeakRef.get(), childEl);
});
|