michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: var traceback = require("sdk/console/traceback"); michael@0: var {Cc,Ci,Cr,Cu} = require("chrome"); michael@0: const { on, off } = require("sdk/system/events"); michael@0: michael@0: function throwNsIException() { michael@0: var ios = Cc['@mozilla.org/network/io-service;1'] michael@0: .getService(Ci.nsIIOService); michael@0: ios.newURI("i'm a malformed URI", null, null); michael@0: } michael@0: michael@0: function throwError() { michael@0: throw new Error("foob"); michael@0: } michael@0: michael@0: exports.testFormatDoesNotFetchRemoteFiles = function(assert) { michael@0: ["http", "https"].forEach( michael@0: function(scheme) { michael@0: var httpRequests = 0; michael@0: function onHttp() { michael@0: httpRequests++; michael@0: } michael@0: michael@0: on("http-on-modify-request", onHttp); michael@0: michael@0: try { michael@0: var tb = [{filename: scheme + "://www.mozilla.org/", michael@0: lineNumber: 1, michael@0: name: "blah"}]; michael@0: traceback.format(tb); michael@0: } catch (e) { michael@0: assert.fail(e); michael@0: } michael@0: michael@0: off("http-on-modify-request", onHttp); michael@0: michael@0: assert.equal(httpRequests, 0, michael@0: "traceback.format() does not make " + michael@0: scheme + " request"); michael@0: }); michael@0: }; michael@0: michael@0: exports.testFromExceptionWithString = function(assert) { michael@0: try { michael@0: throw "foob"; michael@0: assert.fail("an exception should've been thrown"); michael@0: } catch (e) { michael@0: if (e == "foob") { michael@0: var tb = traceback.fromException(e); michael@0: assert.equal(tb.length, 0); michael@0: } michael@0: else { michael@0: throw e; michael@0: } michael@0: } michael@0: }; michael@0: michael@0: exports.testFormatWithString = function(assert) { michael@0: // This can happen if e.g. a thrown exception was michael@0: // a string instead of an Error instance. michael@0: assert.equal(traceback.format("blah"), michael@0: "Traceback (most recent call last):"); michael@0: }; michael@0: michael@0: exports.testFromExceptionWithError = function(assert) { michael@0: try { michael@0: throwError(); michael@0: assert.fail("an exception should've been thrown"); michael@0: } catch (e) { michael@0: if (e instanceof Error) { michael@0: var tb = traceback.fromException(e); michael@0: michael@0: var xulApp = require("sdk/system/xul-app"); michael@0: assert.equal(tb.slice(-1)[0].name, "throwError"); michael@0: } michael@0: else { michael@0: throw e; michael@0: } michael@0: } michael@0: }; michael@0: michael@0: exports.testFromExceptionWithNsIException = function(assert) { michael@0: try { michael@0: throwNsIException(); michael@0: assert.fail("an exception should've been thrown"); michael@0: } catch (e) { michael@0: if (e.result == Cr.NS_ERROR_MALFORMED_URI) { michael@0: var tb = traceback.fromException(e); michael@0: assert.equal(tb[tb.length - 1].name, "throwNsIException"); michael@0: } michael@0: else { michael@0: throw e; michael@0: } michael@0: } michael@0: }; michael@0: michael@0: exports.testFormat = function(assert) { michael@0: function getTraceback() { michael@0: return traceback.format(); michael@0: } michael@0: michael@0: var formatted = getTraceback(); michael@0: assert.equal(typeof(formatted), "string"); michael@0: var lines = formatted.split("\n"); michael@0: michael@0: assert.equal(lines[lines.length - 2].indexOf("getTraceback") > 0, michael@0: true, michael@0: "formatted traceback should include function name"); michael@0: michael@0: assert.equal(lines[lines.length - 1].trim(), michael@0: "return traceback.format();", michael@0: "formatted traceback should include source code"); michael@0: }; michael@0: michael@0: exports.testExceptionsWithEmptyStacksAreLogged = function(assert) { michael@0: // Ensures that our fix to bug 550368 works. michael@0: var sandbox = Cu.Sandbox("http://www.foo.com"); michael@0: var excRaised = false; michael@0: try { michael@0: Cu.evalInSandbox("returns 1 + 2;", sandbox, "1.8", michael@0: "blah.js", 25); michael@0: } catch (e) { michael@0: excRaised = true; michael@0: var stack = traceback.fromException(e); michael@0: assert.equal(stack.length, 1, "stack should have one frame"); michael@0: michael@0: assert.ok(stack[0].fileName, "blah.js", "frame should have filename"); michael@0: assert.ok(stack[0].lineNumber, 25, "frame should have line no"); michael@0: assert.equal(stack[0].name, null, "frame should have null function name"); michael@0: } michael@0: if (!excRaised) michael@0: assert.fail("Exception should have been raised."); michael@0: }; michael@0: michael@0: require('sdk/test').run(exports);