browser/devtools/webconsole/test/browser_webconsole_jsterm.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial