1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/webconsole/test/browser_webconsole_jsterm.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,182 @@ 1.4 +/* vim:set ts=2 sw=2 sts=2 et: */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +const TEST_URI = "http://example.com/browser/browser/devtools/webconsole/test/test-console.html"; 1.10 + 1.11 +let jsterm, testDriver; 1.12 + 1.13 +function test() { 1.14 + addTab(TEST_URI); 1.15 + browser.addEventListener("load", function onLoad() { 1.16 + browser.removeEventListener("load", onLoad, true); 1.17 + openConsole(null, function(hud) { 1.18 + testDriver = testJSTerm(hud); 1.19 + testDriver.next(); 1.20 + }); 1.21 + }, true); 1.22 +} 1.23 + 1.24 +function nextTest() { 1.25 + testDriver.next(); 1.26 +} 1.27 + 1.28 +function checkResult(msg, desc) { 1.29 + waitForMessages({ 1.30 + webconsole: jsterm.hud.owner, 1.31 + messages: [{ 1.32 + name: desc, 1.33 + category: CATEGORY_OUTPUT, 1.34 + }], 1.35 + }).then(([result]) => { 1.36 + let node = [...result.matched][0].querySelector(".message-body"); 1.37 + if (typeof msg == "string") { 1.38 + is(node.textContent.trim(), msg, 1.39 + "correct message shown for " + desc); 1.40 + } 1.41 + else if (typeof msg == "function") { 1.42 + ok(msg(node), "correct message shown for " + desc); 1.43 + } 1.44 + 1.45 + nextTest(); 1.46 + }); 1.47 +} 1.48 + 1.49 +function testJSTerm(hud) 1.50 +{ 1.51 + jsterm = hud.jsterm; 1.52 + const HELP_URL = "https://developer.mozilla.org/docs/Tools/Web_Console/Helpers"; 1.53 + 1.54 + jsterm.clearOutput(); 1.55 + jsterm.execute("$('#header').getAttribute('id')"); 1.56 + checkResult('"header"', "$() worked"); 1.57 + yield undefined; 1.58 + 1.59 + jsterm.clearOutput(); 1.60 + jsterm.execute("$$('h1').length"); 1.61 + checkResult("1", "$$() worked"); 1.62 + yield undefined; 1.63 + 1.64 + jsterm.clearOutput(); 1.65 + jsterm.execute("$x('.//*', document.body)[0] == $$('h1')[0]"); 1.66 + checkResult("true", "$x() worked"); 1.67 + yield undefined; 1.68 + 1.69 + // no jsterm.clearOutput() here as we clear the output using the clear() fn. 1.70 + jsterm.execute("clear()"); 1.71 + 1.72 + waitForSuccess({ 1.73 + name: "clear() worked", 1.74 + validatorFn: function() 1.75 + { 1.76 + return jsterm.outputNode.childNodes.length == 0; 1.77 + }, 1.78 + successFn: nextTest, 1.79 + failureFn: nextTest, 1.80 + }); 1.81 + 1.82 + yield undefined; 1.83 + 1.84 + jsterm.clearOutput(); 1.85 + jsterm.execute("keys({b:1})[0] == 'b'"); 1.86 + checkResult("true", "keys() worked", 1); 1.87 + yield undefined; 1.88 + 1.89 + jsterm.clearOutput(); 1.90 + jsterm.execute("values({b:1})[0] == 1"); 1.91 + checkResult("true", "values() worked", 1); 1.92 + yield undefined; 1.93 + 1.94 + jsterm.clearOutput(); 1.95 + 1.96 + let openedLinks = 0; 1.97 + let onExecuteCalls = 0; 1.98 + let oldOpenLink = hud.openLink; 1.99 + hud.openLink = (url) => { 1.100 + if (url == HELP_URL) { 1.101 + openedLinks++; 1.102 + } 1.103 + }; 1.104 + 1.105 + function onExecute() { 1.106 + onExecuteCalls++; 1.107 + if (onExecuteCalls == 3) { 1.108 + nextTest(); 1.109 + } 1.110 + } 1.111 + 1.112 + jsterm.execute("help()", onExecute); 1.113 + jsterm.execute("help", onExecute); 1.114 + jsterm.execute("?", onExecute); 1.115 + yield undefined; 1.116 + 1.117 + let output = jsterm.outputNode.querySelector(".message[category='output']"); 1.118 + ok(!output, "no output for help() calls"); 1.119 + is(openedLinks, 3, "correct number of pages opened by the help calls"); 1.120 + hud.openLink = oldOpenLink; 1.121 + 1.122 + jsterm.clearOutput(); 1.123 + jsterm.execute("pprint({b:2, a:1})"); 1.124 + checkResult("\" b: 2\n a: 1\"", "pprint()"); 1.125 + yield undefined; 1.126 + 1.127 + // check instanceof correctness, bug 599940 1.128 + jsterm.clearOutput(); 1.129 + jsterm.execute("[] instanceof Array"); 1.130 + checkResult("true", "[] instanceof Array == true"); 1.131 + yield undefined; 1.132 + 1.133 + jsterm.clearOutput(); 1.134 + jsterm.execute("({}) instanceof Object"); 1.135 + checkResult("true", "({}) instanceof Object == true"); 1.136 + yield undefined; 1.137 + 1.138 + // check for occurrences of Object XRayWrapper, bug 604430 1.139 + jsterm.clearOutput(); 1.140 + jsterm.execute("document"); 1.141 + checkResult(function(node) { 1.142 + return node.textContent.search(/\[object xraywrapper/i) == -1; 1.143 + }, "document - no XrayWrapper"); 1.144 + yield undefined; 1.145 + 1.146 + // check that pprint(window) and keys(window) don't throw, bug 608358 1.147 + jsterm.clearOutput(); 1.148 + jsterm.execute("pprint(window)"); 1.149 + checkResult(null, "pprint(window)"); 1.150 + yield undefined; 1.151 + 1.152 + jsterm.clearOutput(); 1.153 + jsterm.execute("keys(window)"); 1.154 + checkResult(null, "keys(window)"); 1.155 + yield undefined; 1.156 + 1.157 + // bug 614561 1.158 + jsterm.clearOutput(); 1.159 + jsterm.execute("pprint('hi')"); 1.160 + checkResult("\" 0: \"h\"\n 1: \"i\"\"", "pprint('hi')"); 1.161 + yield undefined; 1.162 + 1.163 + // check that pprint(function) shows function source, bug 618344 1.164 + jsterm.clearOutput(); 1.165 + jsterm.execute("pprint(print)"); 1.166 + checkResult(function(node) { 1.167 + return node.textContent.indexOf("aOwner.helperResult") > -1; 1.168 + }, "pprint(function) shows source"); 1.169 + yield undefined; 1.170 + 1.171 + // check that an evaluated null produces "null", bug 650780 1.172 + jsterm.clearOutput(); 1.173 + jsterm.execute("null"); 1.174 + checkResult("null", "null is null"); 1.175 + yield undefined; 1.176 + 1.177 + jsterm.clearOutput(); 1.178 + jsterm.execute("undefined"); 1.179 + checkResult("undefined", "undefined is printed"); 1.180 + yield undefined; 1.181 + 1.182 + jsterm = testDriver = null; 1.183 + executeSoon(finishTest); 1.184 + yield undefined; 1.185 +}