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
|
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf8">
<title>Test for Web Console commands registration.</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 Web Console commands registration.</p>
<p id="quack"></p>
<script class="testbody" type="text/javascript;version=1.8">
SimpleTest.waitForExplicitFinish();
let gState;
let tests;
let {WebConsoleCommands} = require("devtools/server/actors/utils/webconsole-utils");
function evaluateJS(input) {
return new Promise((resolve) => gState.client.evaluateJS(input, resolve));
}
function* evaluateJSAndCheckResult(input, result) {
let response = yield evaluateJS(input);
checkObject(response, {result});
}
function startTest()
{
removeEventListener("load", startTest);
attachConsoleToTab(["PageError"], onAttach);
}
function onAttach(aState, aResponse)
{
gState = aState;
runTests(tests, testEnd);
}
tests = [
Task.async(function* registerNewCommand() {
let win;
WebConsoleCommands.register("setFoo", (owner, value) => {
owner.window.foo = value;
return "ok";
});
ok(WebConsoleCommands.hasCommand("setFoo"),
"The command should be registered");
let command = "setFoo('bar')";
let response = yield evaluateJS(command);
checkObject(response, {
from: gState.actor,
input: command,
result: "ok"
});
is(top.foo, "bar", "top.foo should equal to 'bar'");
nextTest();
}),
Task.async(function* wrapCommand() {
let origKeys = WebConsoleCommands.getCommand("keys");
let newKeys = (...args) => {
let [owner, arg0] = args;
if (arg0 === ">o_/") {
return "bang!";
}
else {
return origKeys(...args);
}
};
WebConsoleCommands.register("keys", newKeys);
is(WebConsoleCommands.getCommand("keys"), newKeys,
"the keys() command should have been replaced");
let response = yield evaluateJS("keys('>o_/')");
checkObject(response, {
from: gState.actor,
result: "bang!"
});
response = yield evaluateJS("keys({foo: 'bar'})");
checkObject(response, {
from: gState.actor,
result: {
class: "Array",
preview: {
items: ["foo"]
}
}
});
WebConsoleCommands.register("keys", origKeys);
is(WebConsoleCommands.getCommand("keys"), origKeys,
"the keys() command should be restored");
nextTest();
}),
Task.async(function* unregisterCommand() {
WebConsoleCommands.unregister("setFoo");
let response = yield evaluateJS("setFoo");
checkObject(response, {
from: gState.actor,
input: "setFoo",
result: {
type: "undefined"
},
exceptionMessage: /setFoo is not defined/
});
nextTest();
}),
Task.async(function* registerAccessor() {
WebConsoleCommands.register("$foo", {
get(owner) {
let foo = owner.window.frames[0].window.document.getElementById("quack");
return owner.makeDebuggeeValue(foo);
}
});
let command = "$foo.textContent = '>o_/'";
let response = yield evaluateJS(command);
checkObject(response, {
from: gState.actor,
input: command,
result: ">o_/"
});
is(document.getElementById("quack").textContent, ">o_/",
"#foo textContent should equal to \">o_/\"");
WebConsoleCommands.unregister("$foo");
ok(!WebConsoleCommands.hasCommand("$foo"), "$foo should be unregistered");
nextTest();
}),
Task.async(function* unregisterAfterOverridingTwice() {
WebConsoleCommands.register("keys", (owner, obj) => "command 1");
info("checking the value of the first override");
yield evaluateJSAndCheckResult("keys('foo');", "command 1");
let orig = WebConsoleCommands.getCommand("keys");
WebConsoleCommands.register("keys", (owner, obj) => {
if (obj === "quack")
return "bang!";
return orig(owner, obj);
});
info("checking the values after the second override");
yield evaluateJSAndCheckResult("keys({});", "command 1");
yield evaluateJSAndCheckResult("keys('quack');", "bang!");
WebConsoleCommands.unregister("keys");
info("checking the value after unregistration (should restore " +
"the original command)");
yield evaluateJSAndCheckResult("keys({});", {
class: "Array",
preview: {items: []}
});
nextTest();
})
];
function testEnd()
{
// If this is the first run, reload the page and do it again.
// Otherwise, end the test.
delete top.foo;
closeDebugger(gState, function() {
gState = null;
SimpleTest.finish();
});
}
addEventListener("load", startTest);
</script>
</body>
</html>
|