Test loosely connected to bug 617870 - check whether JSD's topLevelHook and functionHook callbacks are invoked properly (r=test)

This commit is contained in:
Steve Fink 2010-12-10 11:39:46 -08:00
Родитель 8248eb690c
Коммит e052c80510
4 изменённых файлов: 257 добавлений и 2 удалений

Просмотреть файл

@ -47,8 +47,8 @@ MODULE = jsdebug
include $(topsrcdir)/config/rules.mk
_TEST_FILES = test_bug507448.html \
bug507448.js \
_TEST_FILES = test_bug507448.html bug507448.js \
test_bug617870-callhooks.html test-bug617870-callhooks.js jsd-test.js \
$(NULL)
libs:: $(_TEST_FILES)

119
js/jsd/test/jsd-test.js Normal file
Просмотреть файл

@ -0,0 +1,119 @@
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
const Cc = Components.classes;
const Ci = Components.interfaces;
const RETURN_CONTINUE = Ci.jsdIExecutionHook.RETURN_CONTINUE;
const DebuggerService = Cc["@mozilla.org/js/jsd/debugger-service;1"];
var jsd = Components.classes['@mozilla.org/js/jsd/debugger-service;1']
.getService(Ci.jsdIDebuggerService);
var jsdOnAtStart = false;
function setupJSD(test) {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
jsdOnAtStart = jsd.isOn;
if (jsdOnAtStart) {
runTest();
} else {
jsd.asyncOn({ onDebuggerActivated: function() { runTest(); } });
}
}
// Ugly workaround: when you turn the debugger on, it will only see scripts
// compiled after that point. And it may be turned on asynchronously. So
// we put the debugged code into a separate script that gets loaded after
// the debugger is on.
function loadScript(url, element) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.defer = false;
element.appendChild(script);
}
function findScriptByFunction(name) {
var script;
jsd.enumerateScripts({ enumerateScript:
function(script_) {
if (script_.functionName === name) {
script = script_;
}
}
});
if (typeof(script) === "undefined") {
throw("Cannot find function named '" + name + "'");
}
return script;
}
// Pass in a JSD script
function breakOnAllLines(script) {
// Map each line to a PC, and collect that set of PCs (removing
// duplicates.)
var pcs = {};
for (i = 0; i < script.lineExtent; i++) {
var jsdLine = script.baseLineNumber + i;
var pc = script.lineToPc(jsdLine, Ci.jsdIScript.PCMAP_SOURCETEXT);
pcs[pc] = 1;
}
// Set a breakpoint on each of those PCs.
for (pc in pcs) {
try {
script.setBreakpoint(pc);
} catch(e) {
alert("Error setting breakpoint: " + e);
}
}
}
// Set a breakpoint on a script, where lineno is relative to the beginning
// of the script (NOT the absolute line number within the file).
function breakOnLine(script, lineno) {
breakOnAbsoluteLine(script, script.baseLineNumber + lineno);
}
function breakOnAbsoluteLine(script, lineno) {
var pc = script.lineToPc(lineno, Ci.jsdIScript.PCMAP_SOURCETEXT);
script.setBreakpoint(pc);
}
function loadPage(page) {
var url;
if (page.match(/^\w+:/)) {
// Full URI, so just use it
url = page;
} else {
// Treat as relative to previous page
url = document.location.href.replace(/\/[^\/]*$/, "/" + page);
}
dump("Switching to URL " + url + "\n");
gURLBar.value = url;
gURLBar.handleCommand();
}
function breakpointObserver(lines, interesting, callback) {
jsd.breakpointHook = { onExecute: function(frame, type, rv) {
breakpoints_hit.push(frame.line);
if (frame.line in interesting) {
return callback(frame, type, breakpoints_hit);
} else {
return RETURN_CONTINUE;
}
} };
}
function dumpStack(frame, msg) {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
dump(msg + ":\n");
while(frame) {
var callee = frame.callee;
if (callee !== null)
callee = callee.jsClassName;
dump(" " + frame.script.fileName + ":" + frame.line + " func=" + frame.script.functionName + " ffunc=" + frame.functionName + " callee=" + callee + " pc=" + frame.pc + "\n");
frame = frame.callingFrame;
}
}

Просмотреть файл

@ -0,0 +1,52 @@
g = { 'global noneval': 1 };
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
eval("g['global eval'] = 1");
// Function to step through and set breakpoints on
function f1() {
g['function noneval'] = 1;
eval("g['function eval'] = 1");
x = 1;
for (y = 0; y < 10; y++) {
x++;
}
for (y = 0; y < 3; y++) {
x++;
}
z = 3;
}
var f2 = new Function("g['function noneval'] = 2; eval(\"g['function eval'] = 2\")");
function testJSD(jsd) {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
ok(jsd.isOn, "JSD needs to be running for this test.");
var numBreakpoints = 0;
f1();
f2();
jsd.topLevelHook = null;
jsd.functionHook = null;
dump("numGlobalNonevals="+numGlobalNonevals+"\n");
dump("numFunctionNonevals="+numFunctionNonevals+"\n");
dump("numGlobalEvals="+numGlobalEvals+"\n");
dump("numFunctionEvals="+numFunctionEvals+"\n");
ok(numFunctionNonevals == 3, "(fn) Should have hit f1(), testJSD(), and f2(); hit " + hits.fn);
ok(numGlobalNonevals == 1, "(gn) Overall script, hit " + hits.gn);
ok(numGlobalEvals == 1, "(ge) Eval in global area, hit " + hits.ge);
ok(numFunctionEvals == 2, "(fe) Evals within f1() and f2(), hit " + hits.fe);
if (!jsdOnAtStart) {
// turn JSD off if it wasn't on when this test started
jsd.off();
ok(!jsd.isOn, "JSD shouldn't be running at the end of this test.");
}
SimpleTest.finish();
}
testJSD(jsd);

Просмотреть файл

@ -0,0 +1,84 @@
<!DOCTYPE HTML>
<html>
<head>
<!-- The bug number is pulled from the test URL -->
<title>JSD Test for Bug AUTOFILLED</title>
<script type="application/javascript" src="/MochiKit/packed.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript" src="jsd-test.js"></script>
<script type="application/javascript">
var BUG = 617870;
var TEST_SCRIPT = "test-bug617870-callhooks.js";
document.getElementsByTagName("title")[0].innerHTML = "JSD Test for Bug " + BUG;
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
SimpleTest.waitForExplicitFinish();
var hits = { gn: [], ge: [], fn: [], fe: [] };
var numGlobalNonevals = 0;
var numFunctionNonevals = 0;
var numGlobalEvals = 0;
var numFunctionEvals = 0;
function runTest() {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
jsd.topLevelHook = {
onCall: function(frame,type) {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
if (frame.script.fileName.indexOf(TEST_SCRIPT) != -1) {
var desc = frame.script.fileName + ":" + frame.line + " (" + frame.functionName + ")";
if (type == Ci.jsdICallHook.TYPE_TOPLEVEL_START) {
if (frame.callingFrame === null) {
numGlobalNonevals++;
hits.gn.push(desc);
} else if (frame.callee === null) {
numGlobalEvals++;
hits.ge.push(desc);
} else {
numFunctionEvals++;
hits.fe.push(desc);
}
}
dumpStack(frame, "TOPLEVEL(" + type + ")");
}
}
};
jsd.functionHook = {
onCall: function(frame,type) {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
if (frame.script.fileName.indexOf(TEST_SCRIPT) != -1) {
if (type == Ci.jsdICallHook.TYPE_FUNCTION_CALL) {
var desc = frame.script.fileName + ":" + frame.line + " (" + frame.functionName + ")";
numFunctionNonevals++;
hits.fn.push(desc);
}
dumpStack(frame, "FUNCTION(" + type + ")");
}
}
};
loadScript(TEST_SCRIPT, document.getElementById("test"));
}
function setupTest() {
var buglink = document.getElementById("buglink");
buglink.href = "https://bugzilla.mozilla.org/show_bug.cgi?id=" + BUG;
buglink.innerHTML = "Mozilla Bug " + BUG;
}
</script>
</head>
<body onLoad='setupTest(); setupJSD();'>
<a id="buglink" target="_blank"></a>
<p id="display"></p>
<div id="content" style="display: none">
<pre id='test'>
</pre>
</div>
<div id='test-output'>
</div>
</body>
</html>