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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
Services.scriptloader.loadSubScript(
"chrome://mochitests/content/browser/devtools/server/tests/browser/inspector-helpers.js",
this
);
// Test for Bug 835896
// WalkerSearch specific tests. This is to make sure search results are
// coming back as expected.
// See also test_inspector-search-front.html.
add_task(async function () {
const { walker } = await initInspectorFront(
MAIN_DOMAIN + "inspector-search-data.html"
);
await SpecialPowers.spawn(
gBrowser.selectedBrowser,
[[walker.actorID]],
async function (actorID) {
const { require } = ChromeUtils.importESModule(
"resource://devtools/shared/loader/Loader.sys.mjs"
);
const {
DevToolsServer,
} = require("resource://devtools/server/devtools-server.js");
const {
DocumentWalker: _documentWalker,
} = require("resource://devtools/server/actors/inspector/document-walker.js");
// Convert actorID to current compartment string otherwise
// searchAllConnectionsForActor is confused and won't find the actor.
actorID = String(actorID);
const walkerActor = DevToolsServer.searchAllConnectionsForActor(actorID);
const walkerSearch = walkerActor.walkerSearch;
const {
WalkerSearch,
WalkerIndex,
} = require("resource://devtools/server/actors/utils/walker-search.js");
info("Testing basic index APIs exist.");
const index = new WalkerIndex(walkerActor);
Assert.greater(
index.data.size,
0,
"public index is filled after getting"
);
index.clearIndex();
ok(!index._data, "private index is empty after clearing");
Assert.greater(
index.data.size,
0,
"public index is filled after getting"
);
index.destroy();
info("Testing basic search APIs exist.");
ok(walkerSearch, "walker search exists on the WalkerActor");
ok(walkerSearch.search, "walker search has `search` method");
ok(walkerSearch.index, "walker search has `index` property");
is(
walkerSearch.walker,
walkerActor,
"referencing the correct WalkerActor"
);
const walkerSearch2 = new WalkerSearch(walkerActor);
ok(walkerSearch2, "a new search instance can be created");
ok(walkerSearch2.search, "new search instance has `search` method");
ok(walkerSearch2.index, "new search instance has `index` property");
isnot(
walkerSearch2,
walkerSearch,
"new search instance differs from the WalkerActor's"
);
walkerSearch2.destroy();
info("Testing search with an empty query.");
let results = walkerSearch.search("");
is(results.length, 0, "No results when searching for ''");
results = walkerSearch.search(null);
is(results.length, 0, "No results when searching for null");
results = walkerSearch.search(undefined);
is(results.length, 0, "No results when searching for undefined");
results = walkerSearch.search(10);
is(results.length, 0, "No results when searching for 10");
const inspectee = content.document;
const testData = [
{
desc: "Search for tag with one result.",
search: "body",
expected: [{ node: inspectee.body, type: "tag" }],
},
{
desc: "Search for tag with multiple results",
search: "h2",
expected: [
{ node: inspectee.querySelectorAll("h2")[0], type: "tag" },
{ node: inspectee.querySelectorAll("h2")[1], type: "tag" },
{ node: inspectee.querySelectorAll("h2")[2], type: "tag" },
],
},
{
desc: "Search for selector with multiple results",
search: "body > h2",
expected: [
{ node: inspectee.querySelectorAll("h2")[0], type: "selector" },
{ node: inspectee.querySelectorAll("h2")[1], type: "selector" },
{ node: inspectee.querySelectorAll("h2")[2], type: "selector" },
],
},
{
desc: "Search for selector with multiple results",
search: ":root h2",
expected: [
{ node: inspectee.querySelectorAll("h2")[0], type: "selector" },
{ node: inspectee.querySelectorAll("h2")[1], type: "selector" },
{ node: inspectee.querySelectorAll("h2")[2], type: "selector" },
],
},
{
desc: "Search for selector with multiple results",
search: "* h2",
expected: [
{ node: inspectee.querySelectorAll("h2")[0], type: "selector" },
{ node: inspectee.querySelectorAll("h2")[1], type: "selector" },
{ node: inspectee.querySelectorAll("h2")[2], type: "selector" },
],
},
{
desc: "Search with multiple matches in a single tag expecting a single result",
search: "💩",
expected: [
{ node: inspectee.getElementById("💩"), type: "attributeValue" },
],
},
{
desc: "Search that has tag and text results",
search: "h1",
expected: [
{ node: inspectee.querySelector("h1"), type: "tag" },
{
node: inspectee.querySelector("h1 + p").childNodes[0],
type: "text",
},
{
node: inspectee.querySelector("h1 + p > strong").childNodes[0],
type: "text",
},
],
},
{
desc: "Search for XPath with one result",
search: "//strong",
expected: [
{ node: inspectee.querySelector("strong"), type: "xpath" },
],
},
{
desc: "Search for XPath with multiple results",
search: "//h2",
expected: [
{ node: inspectee.querySelectorAll("h2")[0], type: "xpath" },
{ node: inspectee.querySelectorAll("h2")[1], type: "xpath" },
{ node: inspectee.querySelectorAll("h2")[2], type: "xpath" },
],
},
{
desc: "Search for XPath via containing text",
search: "//*[contains(text(), 'p tag')]",
expected: [{ node: inspectee.querySelector("p"), type: "xpath" }],
},
{
desc: "Search for XPath matching text node",
search: "//strong/text()",
expected: [
{
node: inspectee.querySelector("strong").firstChild,
type: "xpath",
},
],
},
{
desc: "Search using XPath grouping expression",
search: "(//*)[2]",
expected: [{ node: inspectee.querySelector("head"), type: "xpath" }],
},
{
desc: "Search using XPath function",
search: "id('arrows')",
expected: [
{ node: inspectee.querySelector("#arrows"), type: "xpath" },
],
},
];
const isDeeply = (a, b, msg) => {
return is(JSON.stringify(a), JSON.stringify(b), msg);
};
for (const { desc, search, expected } of testData) {
info("Running test: " + desc);
results = walkerSearch.search(search);
isDeeply(
results,
expected,
"Search returns correct results with '" + search + "'"
);
}
info("Testing ::before and ::after element matching");
const beforeElt = new _documentWalker(
inspectee.querySelector("#pseudo"),
inspectee.defaultView
).firstChild();
const afterElt = new _documentWalker(
inspectee.querySelector("#pseudo"),
inspectee.defaultView
).lastChild();
const styleText = inspectee.querySelector("style").childNodes[0];
// ::before
results = walkerSearch.search("::before");
isDeeply(
results,
[{ node: beforeElt, type: "tag" }],
"Tag search works for pseudo element"
);
results = walkerSearch.search("_moz_generated_content_before");
is(results.length, 0, "No results for anon tag name");
results = walkerSearch.search("before element");
isDeeply(
results,
[
{ node: styleText, type: "text" },
{ node: beforeElt, type: "text" },
],
"Text search works for pseudo element"
);
// ::after
results = walkerSearch.search("::after");
isDeeply(
results,
[{ node: afterElt, type: "tag" }],
"Tag search works for pseudo element"
);
results = walkerSearch.search("_moz_generated_content_after");
is(results.length, 0, "No results for anon tag name");
results = walkerSearch.search("after element");
isDeeply(
results,
[
{ node: styleText, type: "text" },
{ node: afterElt, type: "text" },
],
"Text search works for pseudo element"
);
info("Testing search before and after a mutation.");
const expected = [
{ node: inspectee.querySelectorAll("h3")[0], type: "tag" },
{ node: inspectee.querySelectorAll("h3")[1], type: "tag" },
{ node: inspectee.querySelectorAll("h3")[2], type: "tag" },
];
results = walkerSearch.search("h3");
isDeeply(results, expected, "Search works with tag results");
function mutateDocumentAndWaitForMutation(mutationFn) {
// eslint-disable-next-line new-cap
return new Promise(resolve => {
info("Listening to markup mutation on the inspectee");
const observer = new inspectee.defaultView.MutationObserver(resolve);
observer.observe(inspectee, { childList: true, subtree: true });
mutationFn();
});
}
await mutateDocumentAndWaitForMutation(() => {
expected[0].node.remove();
});
results = walkerSearch.search("h3");
isDeeply(
results,
[expected[1], expected[2]],
"Results are updated after removal"
);
// eslint-disable-next-line new-cap
await new Promise(resolve => {
info("Waiting for a mutation to happen");
const observer = new inspectee.defaultView.MutationObserver(() => {
resolve();
});
observer.observe(inspectee, { attributes: true, subtree: true });
inspectee.body.setAttribute("h3", "true");
});
results = walkerSearch.search("h3");
isDeeply(
results,
[
{ node: inspectee.body, type: "attributeName" },
expected[1],
expected[2],
],
"Results are updated after addition"
);
// eslint-disable-next-line new-cap
await new Promise(resolve => {
info("Waiting for a mutation to happen");
const observer = new inspectee.defaultView.MutationObserver(() => {
resolve();
});
observer.observe(inspectee, {
attributes: true,
childList: true,
subtree: true,
});
inspectee.body.removeAttribute("h3");
expected[1].node.remove();
expected[2].node.remove();
});
results = walkerSearch.search("h3");
is(results.length, 0, "Results are updated after removal");
}
);
});
|