browser/devtools/webconsole/test/browser_webconsole_jsterm.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:736b65c06b4e
1 /* vim:set ts=2 sw=2 sts=2 et: */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 const TEST_URI = "http://example.com/browser/browser/devtools/webconsole/test/test-console.html";
7
8 let jsterm, testDriver;
9
10 function test() {
11 addTab(TEST_URI);
12 browser.addEventListener("load", function onLoad() {
13 browser.removeEventListener("load", onLoad, true);
14 openConsole(null, function(hud) {
15 testDriver = testJSTerm(hud);
16 testDriver.next();
17 });
18 }, true);
19 }
20
21 function nextTest() {
22 testDriver.next();
23 }
24
25 function checkResult(msg, desc) {
26 waitForMessages({
27 webconsole: jsterm.hud.owner,
28 messages: [{
29 name: desc,
30 category: CATEGORY_OUTPUT,
31 }],
32 }).then(([result]) => {
33 let node = [...result.matched][0].querySelector(".message-body");
34 if (typeof msg == "string") {
35 is(node.textContent.trim(), msg,
36 "correct message shown for " + desc);
37 }
38 else if (typeof msg == "function") {
39 ok(msg(node), "correct message shown for " + desc);
40 }
41
42 nextTest();
43 });
44 }
45
46 function testJSTerm(hud)
47 {
48 jsterm = hud.jsterm;
49 const HELP_URL = "https://developer.mozilla.org/docs/Tools/Web_Console/Helpers";
50
51 jsterm.clearOutput();
52 jsterm.execute("$('#header').getAttribute('id')");
53 checkResult('"header"', "$() worked");
54 yield undefined;
55
56 jsterm.clearOutput();
57 jsterm.execute("$$('h1').length");
58 checkResult("1", "$$() worked");
59 yield undefined;
60
61 jsterm.clearOutput();
62 jsterm.execute("$x('.//*', document.body)[0] == $$('h1')[0]");
63 checkResult("true", "$x() worked");
64 yield undefined;
65
66 // no jsterm.clearOutput() here as we clear the output using the clear() fn.
67 jsterm.execute("clear()");
68
69 waitForSuccess({
70 name: "clear() worked",
71 validatorFn: function()
72 {
73 return jsterm.outputNode.childNodes.length == 0;
74 },
75 successFn: nextTest,
76 failureFn: nextTest,
77 });
78
79 yield undefined;
80
81 jsterm.clearOutput();
82 jsterm.execute("keys({b:1})[0] == 'b'");
83 checkResult("true", "keys() worked", 1);
84 yield undefined;
85
86 jsterm.clearOutput();
87 jsterm.execute("values({b:1})[0] == 1");
88 checkResult("true", "values() worked", 1);
89 yield undefined;
90
91 jsterm.clearOutput();
92
93 let openedLinks = 0;
94 let onExecuteCalls = 0;
95 let oldOpenLink = hud.openLink;
96 hud.openLink = (url) => {
97 if (url == HELP_URL) {
98 openedLinks++;
99 }
100 };
101
102 function onExecute() {
103 onExecuteCalls++;
104 if (onExecuteCalls == 3) {
105 nextTest();
106 }
107 }
108
109 jsterm.execute("help()", onExecute);
110 jsterm.execute("help", onExecute);
111 jsterm.execute("?", onExecute);
112 yield undefined;
113
114 let output = jsterm.outputNode.querySelector(".message[category='output']");
115 ok(!output, "no output for help() calls");
116 is(openedLinks, 3, "correct number of pages opened by the help calls");
117 hud.openLink = oldOpenLink;
118
119 jsterm.clearOutput();
120 jsterm.execute("pprint({b:2, a:1})");
121 checkResult("\" b: 2\n a: 1\"", "pprint()");
122 yield undefined;
123
124 // check instanceof correctness, bug 599940
125 jsterm.clearOutput();
126 jsterm.execute("[] instanceof Array");
127 checkResult("true", "[] instanceof Array == true");
128 yield undefined;
129
130 jsterm.clearOutput();
131 jsterm.execute("({}) instanceof Object");
132 checkResult("true", "({}) instanceof Object == true");
133 yield undefined;
134
135 // check for occurrences of Object XRayWrapper, bug 604430
136 jsterm.clearOutput();
137 jsterm.execute("document");
138 checkResult(function(node) {
139 return node.textContent.search(/\[object xraywrapper/i) == -1;
140 }, "document - no XrayWrapper");
141 yield undefined;
142
143 // check that pprint(window) and keys(window) don't throw, bug 608358
144 jsterm.clearOutput();
145 jsterm.execute("pprint(window)");
146 checkResult(null, "pprint(window)");
147 yield undefined;
148
149 jsterm.clearOutput();
150 jsterm.execute("keys(window)");
151 checkResult(null, "keys(window)");
152 yield undefined;
153
154 // bug 614561
155 jsterm.clearOutput();
156 jsterm.execute("pprint('hi')");
157 checkResult("\" 0: \"h\"\n 1: \"i\"\"", "pprint('hi')");
158 yield undefined;
159
160 // check that pprint(function) shows function source, bug 618344
161 jsterm.clearOutput();
162 jsterm.execute("pprint(print)");
163 checkResult(function(node) {
164 return node.textContent.indexOf("aOwner.helperResult") > -1;
165 }, "pprint(function) shows source");
166 yield undefined;
167
168 // check that an evaluated null produces "null", bug 650780
169 jsterm.clearOutput();
170 jsterm.execute("null");
171 checkResult("null", "null is null");
172 yield undefined;
173
174 jsterm.clearOutput();
175 jsterm.execute("undefined");
176 checkResult("undefined", "undefined is printed");
177 yield undefined;
178
179 jsterm = testDriver = null;
180 executeSoon(finishTest);
181 yield undefined;
182 }

mercurial