Bug 770145 - GCLI needs a command to log function calls; r=past

This commit is contained in:
Joe Walker 2012-07-13 17:06:46 +01:00
parent 7eaf801170
commit e2d51fe46e
4 changed files with 193 additions and 0 deletions

View File

@ -26,6 +26,16 @@ XPCOMUtils.defineLazyModuleGetter(this, "console",
XPCOMUtils.defineLazyModuleGetter(this, "AddonManager",
"resource://gre/modules/AddonManager.jsm");
XPCOMUtils.defineLazyGetter(this, "Debugger", function() {
let JsDebugger = {};
Components.utils.import("resource://gre/modules/jsdebugger.jsm", JsDebugger);
let global = Components.utils.getGlobalForObject({});
JsDebugger.addDebuggerToGlobal(global);
return global.Debugger;
});
let prefSvc = "@mozilla.org/preferences-service;1";
XPCOMUtils.defineLazyGetter(this, "prefBranch", function() {
let prefService = Cc[prefSvc].getService(Ci.nsIPrefService);
@ -281,6 +291,87 @@ gcli.addCommand({
});
let callLogDebuggers = [];
/**
* 'calllog' command
*/
gcli.addCommand({
name: "calllog",
description: gcli.lookup("calllogDesc")
})
/**
* 'calllog start' command
*/
gcli.addCommand({
name: "calllog start",
description: gcli.lookup("calllogStartDesc"),
exec: function(args, context) {
let contentWindow = context.environment.contentDocument.defaultView;
let dbg = new Debugger(contentWindow);
dbg.onEnterFrame = function(frame) {
// BUG 773652 - Make the output from the GCLI calllog command nicer
contentWindow.console.log("Method call: " + this.callDescription(frame));
}.bind(this);
callLogDebuggers.push(dbg);
let tab = context.environment.chromeDocument.defaultView.gBrowser.selectedTab;
HUDService.activateHUDForContext(tab);
return gcli.lookup("calllogStartReply");
},
callDescription: function(frame) {
let name = "<anonymous>";
if (frame.callee.name) {
name = frame.callee.name;
}
else {
let desc = frame.callee.getOwnPropertyDescriptor("displayName");
if (desc && desc.value && typeof desc.value == "string") {
name = desc.value;
}
}
let args = frame.arguments.map(this.valueToString).join(", ");
return name + "(" + args + ")";
},
valueToString: function(value) {
if (typeof value !== "object" || value === null) {
return uneval(value);
}
return "[object " + value.class + "]";
}
});
/**
* 'calllog stop' command
*/
gcli.addCommand({
name: "calllog stop",
description: gcli.lookup("calllogStopDesc"),
exec: function(args, context) {
let numDebuggers = callLogDebuggers.length;
if (numDebuggers == 0) {
return gcli.lookup("calllogStopNoLogging");
}
for (let dbg of callLogDebuggers) {
dbg.onEnterFrame = undefined;
}
callLogDebuggers = [];
return gcli.lookupFormat("calllogStopReply", [ numDebuggers ]);
}
});
/**
* 'console' command
*/

View File

@ -14,6 +14,7 @@ include $(DEPTH)/config/autoconf.mk
MOCHITEST_BROWSER_FILES = \
browser_gcli_addon.js \
browser_gcli_break.js \
browser_gcli_calllog.js \
browser_gcli_commands.js \
browser_gcli_cookie.js \
browser_gcli_edit.js \

View File

@ -0,0 +1,74 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
// Tests that the calllog commands works as they should
let imported = {};
Components.utils.import("resource:///modules/HUDService.jsm", imported);
const TEST_URI = "data:text/html;charset=utf-8,gcli-calllog";
function test() {
DeveloperToolbarTest.test(TEST_URI, function(browser, tab) {
testCallLogStatus();
testCallLogExec();
finish();
});
}
function testCallLogStatus() {
DeveloperToolbarTest.checkInputStatus({
typed: "calllog",
status: "ERROR"
});
DeveloperToolbarTest.checkInputStatus({
typed: "calllog start",
status: "VALID",
emptyParameters: [ ]
});
DeveloperToolbarTest.checkInputStatus({
typed: "calllog start",
status: "VALID",
emptyParameters: [ ]
});
}
function testCallLogExec() {
DeveloperToolbarTest.exec({
typed: "calllog stop",
args: { },
outputMatch: /No call logging/,
});
DeveloperToolbarTest.exec({
typed: "calllog start",
args: { },
outputMatch: /Call logging started/,
});
let hud = imported.HUDService.getHudByWindow(content);
ok(hud.hudId in imported.HUDService.hudReferences, "console open");
DeveloperToolbarTest.exec({
typed: "calllog stop",
args: { },
outputMatch: /Stopped call logging/,
});
DeveloperToolbarTest.exec({
typed: "console clear",
args: {},
blankOutput: true,
});
let labels = hud.jsterm.outputNode.querySelectorAll(".webconsole-msg-output");
is(labels.length, 0, "no output in console");
DeveloperToolbarTest.exec({
typed: "console close",
args: {},
blankOutput: true,
});
}

View File

@ -691,3 +691,30 @@ cookieSetDomainDesc=The domain of the cookie to set
# 'secure' parameter to the 'cookie set' command, which is displayed in a dialog
# when the user is using this command.
cookieSetSecureDesc=Only transmitted over https
# LOCALIZATION NOTE (calllogDesc) A very short description of the
# 'calllog' command. This string is designed to be shown in a menu
# alongside the command name, which is why it should be as short as possible.
calllogDesc=Commands to manipulate function call logging
# LOCALIZATION NOTE (calllogStartDesc) A very short description of the
# 'calllog start' command. This string is designed to be shown in a menu
# alongside the command name, which is why it should be as short as possible.
calllogStartDesc=Start logging function calls to the console
# LOCALIZATION NOTE (calllogStartReply) A string displayed as the result of
# the 'calllog start' command.
calllogStartReply=Call logging started.
# LOCALIZATION NOTE (calllogStopDesc) A very short description of the
# 'calllog stop' command. This string is designed to be shown in a menu
# alongside the command name, which is why it should be as short as possible.
calllogStopDesc=Stop function call logging
# LOCALIZATION NOTE (calllogStopNoLogging) A string displayed as the result of
# the 'calllog stop' command when there is nothing to stop.
calllogStopNoLogging=No call logging is currently active
# LOCALIZATION NOTE (calllogStopReply) A string displayed as the result of
# the 'calllog stop' command when there are logging actions to stop.
calllogStopReply=Stopped call logging. Active contexts: %1$S.