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
|
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf8">
<title>Test for the querySelector / querySelectorAll helpers</title>
<script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript;version=1.8" src="common.js"></script>
<!-- Any copyright is dedicated to the Public Domain.
- http://creativecommons.org/publicdomain/zero/1.0/ -->
</head>
<body>
<p>Test for the querySelector / querySelectorAll helpers</p>
<script class="testbody" type="text/javascript;version=1.8">
SimpleTest.waitForExplicitFinish();
let gState;
let gWin;
function evaluateJS(input) {
return new Promise((resolve) => gState.client.evaluateJS(input, resolve));
}
function startTest() {
info ("Content window opened, attaching console to it");
let systemPrincipal = Cc["@mozilla.org/systemprincipal;1"].createInstance(Ci.nsIPrincipal);
ok (!gWin.document.nodePrincipal.equals(systemPrincipal),
"The test document is not using the system principal");
attachConsoleToTab([], state => {
gState = state;
let tests = [
setupWindow,
checkQuerySelector,
checkQuerySelectorAll,
checkQuerySelectorAllNotExist,
checkQuerySelectorAllException
];
runTests(tests, testEnd);
});
}
let setupWindow = Task.async(function*() {
info ("Shimming window functions for the content privileged tab");
yield evaluateJS("document.querySelector = function() { throw 'should not call qS'; }");
yield evaluateJS("document.querySelectorAll = function() { throw 'should not call qSA'; }");
nextTest();
});
let checkQuerySelector = Task.async(function*() {
info ("$ returns an DOMNode");
let response = yield evaluateJS("$('body')");
basicResultCheck(response, "$('body')", {
type: "object",
class: "HTMLBodyElement",
preview: {
kind: "DOMNode",
nodeName: "body"
}
});
nextTest();
});
let checkQuerySelectorAll = Task.async(function*() {
info ("$$ returns an array");
let response = yield evaluateJS("$$('body')");
basicResultCheck(response, "$$('body')", {
type: "object",
class: "Array",
preview: {
length: 1
}
});
nextTest();
});
let checkQuerySelectorAllNotExist = Task.async(function*() {
info ("$$ returns an array even if query yields no results");
let response = yield evaluateJS("$$('foobar')");
basicResultCheck(response, "$$('foobar')", {
type: "object",
class: "Array",
preview: {
length: 0
}
});
nextTest();
});
let checkQuerySelectorAllException = Task.async(function*() {
info ("$$ returns an exception if an invalid selector was provided");
let response = yield evaluateJS("$$(':foo')");
checkObject(response, {
input: "$$(':foo')",
exceptionMessage: "SyntaxError: ':foo' is not a valid selector",
exception: {
preview: {
kind: "DOMException",
name: "SyntaxError",
message: "':foo' is not a valid selector"
}
}
});
nextTest();
});
function basicResultCheck(response, input, output) {
checkObject(response, {
from: gState.actor,
input: input,
result: output,
});
ok(!response.exception, "no eval exception");
ok(!response.helperResult, "no helper result");
}
function testEnd() {
gWin.close();
gWin = null;
closeDebugger(gState, function() {
gState = null;
SimpleTest.finish();
});
}
window.onload = function() {
// Open a content window to test XRay functionality on built in functions.
gWin = window.open("data:text/html,");
info ("Waiting for content window to load");
gWin.onload = startTest;
}
</script>
</body>
</html>
|