michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: const TAB_URL = EXAMPLE_URL + "doc_closures.html"; michael@0: michael@0: // Test that inspecting a closure works as expected. michael@0: michael@0: function test() { michael@0: let gPanel, gTab, gDebuggee, gDebugger; michael@0: michael@0: initDebugger(TAB_URL).then(([aTab, aDebuggee, aPanel]) => { michael@0: gTab = aTab; michael@0: gDebuggee = aDebuggee; michael@0: gPanel = aPanel; michael@0: gDebugger = gPanel.panelWin; michael@0: gDebuggee.gRecurseLimit = 2; michael@0: michael@0: waitForSourceShown(gPanel, ".html") michael@0: .then(testClosure) michael@0: .then(() => resumeDebuggerThenCloseAndFinish(gPanel)) michael@0: .then(null, aError => { michael@0: ok(false, "Got an error: " + aError.message + "\n" + aError.stack); michael@0: }); michael@0: }); michael@0: michael@0: function testClosure() { michael@0: // Spin the event loop before causing the debuggee to pause, to allow michael@0: // this function to return first. michael@0: executeSoon(() => { michael@0: EventUtils.sendMouseEvent({ type: "click" }, michael@0: gDebuggee.document.querySelector("button"), michael@0: gDebuggee); michael@0: }); michael@0: michael@0: return waitForDebuggerEvents(gPanel, gDebugger.EVENTS.FETCHED_SCOPES).then(() => { michael@0: let gVars = gDebugger.DebuggerView.Variables; michael@0: let localScope = gVars.getScopeAtIndex(0); michael@0: let localNodes = localScope.target.querySelector(".variables-view-element-details").childNodes; michael@0: michael@0: is(localNodes[4].querySelector(".name").getAttribute("value"), "person", michael@0: "Should have the right property name for |person|."); michael@0: is(localNodes[4].querySelector(".value").getAttribute("value"), "Object", michael@0: "Should have the right property value for |person|."); michael@0: michael@0: // Expand the 'person' tree node. This causes its properties to be michael@0: // retrieved and displayed. michael@0: let personNode = gVars.getItemForNode(localNodes[4]); michael@0: let personFetched = waitForDebuggerEvents(gPanel, gDebugger.EVENTS.FETCHED_PROPERTIES); michael@0: personNode.expand(); michael@0: michael@0: return personFetched.then(() => { michael@0: is(personNode.expanded, true, michael@0: "|person| should be expanded at this point."); michael@0: michael@0: is(personNode.get("getName").target.querySelector(".name") michael@0: .getAttribute("value"), "getName", michael@0: "Should have the right property name for 'getName' in person."); michael@0: is(personNode.get("getName").target.querySelector(".value") michael@0: .getAttribute("value"), "_pfactory/<.getName()", michael@0: "'getName' in person should have the right value."); michael@0: is(personNode.get("getFoo").target.querySelector(".name") michael@0: .getAttribute("value"), "getFoo", michael@0: "Should have the right property name for 'getFoo' in person."); michael@0: is(personNode.get("getFoo").target.querySelector(".value") michael@0: .getAttribute("value"), "_pfactory/<.getFoo()", michael@0: "'getFoo' in person should have the right value."); michael@0: michael@0: // Expand the function nodes. This causes their properties to be michael@0: // retrieved and displayed. michael@0: let getFooNode = personNode.get("getFoo"); michael@0: let getNameNode = personNode.get("getName"); michael@0: let funcsFetched = waitForDebuggerEvents(gPanel, gDebugger.EVENTS.FETCHED_PROPERTIES, 2); michael@0: let funcClosuresFetched = waitForDebuggerEvents(gPanel, gDebugger.EVENTS.FETCHED_SCOPES, 2); michael@0: getFooNode.expand(); michael@0: getNameNode.expand(); michael@0: michael@0: return funcsFetched.then(() => { michael@0: is(getFooNode.expanded, true, michael@0: "|person.getFoo| should be expanded at this point."); michael@0: is(getNameNode.expanded, true, michael@0: "|person.getName| should be expanded at this point."); michael@0: michael@0: is(getFooNode.get("").target.querySelector(".name") michael@0: .getAttribute("value"), "", michael@0: "Found the closure node for getFoo."); michael@0: is(getFooNode.get("").target.querySelector(".value") michael@0: .getAttribute("value"), "", michael@0: "The closure node has no value for getFoo."); michael@0: is(getNameNode.get("").target.querySelector(".name") michael@0: .getAttribute("value"), "", michael@0: "Found the closure node for getName."); michael@0: is(getNameNode.get("").target.querySelector(".value") michael@0: .getAttribute("value"), "", michael@0: "The closure node has no value for getName."); michael@0: michael@0: // Expand the closure nodes. This causes their environments to be michael@0: // retrieved and displayed. michael@0: let getFooClosure = getFooNode.get(""); michael@0: let getNameClosure = getNameNode.get(""); michael@0: getFooClosure.expand(); michael@0: getNameClosure.expand(); michael@0: michael@0: return funcClosuresFetched.then(() => { michael@0: is(getFooClosure.expanded, true, michael@0: "|person.getFoo| closure should be expanded at this point."); michael@0: is(getNameClosure.expanded, true, michael@0: "|person.getName| closure should be expanded at this point."); michael@0: michael@0: is(getFooClosure.get("Function scope [_pfactory]").target.querySelector(".name") michael@0: .getAttribute("value"), "Function scope [_pfactory]", michael@0: "Found the function scope node for the getFoo closure."); michael@0: is(getFooClosure.get("Function scope [_pfactory]").target.querySelector(".value") michael@0: .getAttribute("value"), "", michael@0: "The function scope node has no value for the getFoo closure."); michael@0: is(getNameClosure.get("Function scope [_pfactory]").target.querySelector(".name") michael@0: .getAttribute("value"), "Function scope [_pfactory]", michael@0: "Found the function scope node for the getName closure."); michael@0: is(getNameClosure.get("Function scope [_pfactory]").target.querySelector(".value") michael@0: .getAttribute("value"), "", michael@0: "The function scope node has no value for the getName closure."); michael@0: michael@0: // Expand the scope nodes. michael@0: let getFooInnerScope = getFooClosure.get("Function scope [_pfactory]"); michael@0: let getNameInnerScope = getNameClosure.get("Function scope [_pfactory]"); michael@0: let innerFuncsFetched = waitForDebuggerEvents(gPanel, gDebugger.EVENTS.FETCHED_PROPERTIES, 2); michael@0: getFooInnerScope.expand(); michael@0: getNameInnerScope.expand(); michael@0: michael@0: return funcsFetched.then(() => { michael@0: is(getFooInnerScope.expanded, true, michael@0: "|person.getFoo| inner scope should be expanded at this point."); michael@0: is(getNameInnerScope.expanded, true, michael@0: "|person.getName| inner scope should be expanded at this point."); michael@0: michael@0: // Only test that each function closes over the necessary variable. michael@0: // We wouldn't want future SpiderMonkey closure space michael@0: // optimizations to break this test. michael@0: is(getFooInnerScope.get("foo").target.querySelector(".name") michael@0: .getAttribute("value"), "foo", michael@0: "Found the foo node for the getFoo inner scope."); michael@0: is(getFooInnerScope.get("foo").target.querySelector(".value") michael@0: .getAttribute("value"), "10", michael@0: "The foo node has the expected value."); michael@0: is(getNameInnerScope.get("name").target.querySelector(".name") michael@0: .getAttribute("value"), "name", michael@0: "Found the name node for the getName inner scope."); michael@0: is(getNameInnerScope.get("name").target.querySelector(".value") michael@0: .getAttribute("value"), '"Bob"', michael@0: "The name node has the expected value."); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: } michael@0: }