1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/devtools/server/tests/unit/test_sourcemaps-03.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,149 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +/** 1.8 + * Check setting breakpoints in source mapped sources. 1.9 + */ 1.10 + 1.11 +var gDebuggee; 1.12 +var gClient; 1.13 +var gThreadClient; 1.14 + 1.15 +Components.utils.import('resource:///modules/devtools/SourceMap.jsm'); 1.16 + 1.17 +function run_test() 1.18 +{ 1.19 + initTestDebuggerServer(); 1.20 + gDebuggee = addTestGlobal("test-source-map"); 1.21 + gClient = new DebuggerClient(DebuggerServer.connectPipe()); 1.22 + gClient.connect(function() { 1.23 + attachTestTabAndResume(gClient, "test-source-map", function(aResponse, aTabClient, aThreadClient) { 1.24 + gThreadClient = aThreadClient; 1.25 + test_simple_source_map(); 1.26 + }); 1.27 + }); 1.28 + do_test_pending(); 1.29 +} 1.30 + 1.31 +function testBreakpointMapping(aName, aCallback) 1.32 +{ 1.33 + // Pause so we can set a breakpoint. 1.34 + gThreadClient.addOneTimeListener("paused", function (aEvent, aPacket) { 1.35 + do_check_true(!aPacket.error); 1.36 + do_check_eq(aPacket.why.type, "debuggerStatement"); 1.37 + 1.38 + gThreadClient.setBreakpoint({ 1.39 + url: "http://example.com/www/js/" + aName + ".js", 1.40 + // Setting the breakpoint on an empty line so that it is pushed down one 1.41 + // line and we can check the source mapped actualLocation later. 1.42 + line: 3, 1.43 + column: 0 1.44 + }, function (aResponse) { 1.45 + do_check_true(!aResponse.error); 1.46 + 1.47 + // Actual location should come back source mapped still so that 1.48 + // breakpoints are displayed in the UI correctly, etc. 1.49 + do_check_eq(aResponse.actualLocation.line, 4); 1.50 + do_check_eq(aResponse.actualLocation.url, 1.51 + "http://example.com/www/js/" + aName + ".js"); 1.52 + 1.53 + // The eval will cause us to resume, then we get an unsolicited pause 1.54 + // because of our breakpoint, we resume again to finish the eval, and 1.55 + // finally receive our last pause which has the result of the client 1.56 + // evaluation. 1.57 + gThreadClient.eval(null, aName + "()", function (aResponse) { 1.58 + do_check_true(!aResponse.error, "Shouldn't be an error resuming to eval"); 1.59 + do_check_eq(aResponse.type, "resumed"); 1.60 + 1.61 + gThreadClient.addOneTimeListener("paused", function (aEvent, aPacket) { 1.62 + do_check_eq(aPacket.why.type, "breakpoint"); 1.63 + // Assert that we paused because of the breakpoint at the correct 1.64 + // location in the code by testing that the value of `ret` is still 1.65 + // undefined. 1.66 + do_check_eq(aPacket.frame.environment.bindings.variables.ret.value.type, 1.67 + "undefined"); 1.68 + 1.69 + gThreadClient.resume(function (aResponse) { 1.70 + do_check_true(!aResponse.error); 1.71 + 1.72 + gThreadClient.addOneTimeListener("paused", function (aEvent, aPacket) { 1.73 + do_check_eq(aPacket.why.type, "clientEvaluated"); 1.74 + do_check_eq(aPacket.why.frameFinished.return, aName); 1.75 + 1.76 + gThreadClient.resume(function (aResponse) { 1.77 + do_check_true(!aResponse.error); 1.78 + aCallback(); 1.79 + }); 1.80 + }); 1.81 + }); 1.82 + }); 1.83 + }); 1.84 + }); 1.85 + }); 1.86 + 1.87 + gDebuggee.eval("(" + function () { 1.88 + debugger; 1.89 + } + "());"); 1.90 +} 1.91 + 1.92 +function test_simple_source_map() 1.93 +{ 1.94 + let expectedSources = new Set([ 1.95 + "http://example.com/www/js/a.js", 1.96 + "http://example.com/www/js/b.js", 1.97 + "http://example.com/www/js/c.js" 1.98 + ]); 1.99 + 1.100 + gClient.addListener("newSource", function _onNewSource(aEvent, aPacket) { 1.101 + expectedSources.delete(aPacket.source.url); 1.102 + if (expectedSources.size > 0) { 1.103 + return; 1.104 + } 1.105 + gClient.removeListener("newSource", _onNewSource); 1.106 + 1.107 + testBreakpointMapping("a", function () { 1.108 + testBreakpointMapping("b", function () { 1.109 + testBreakpointMapping("c", function () { 1.110 + finishClient(gClient); 1.111 + }); 1.112 + }); 1.113 + }); 1.114 + }); 1.115 + 1.116 + let a = new SourceNode(null, null, null, [ 1.117 + new SourceNode(1, 0, "a.js", "function a() {\n"), 1.118 + new SourceNode(2, 0, "a.js", " var ret;\n"), 1.119 + new SourceNode(3, 0, "a.js", " // Empty line\n"), 1.120 + new SourceNode(4, 0, "a.js", " ret = 'a';\n"), 1.121 + new SourceNode(5, 0, "a.js", " return ret;\n"), 1.122 + new SourceNode(6, 0, "a.js", "}\n") 1.123 + ]); 1.124 + let b = new SourceNode(null, null, null, [ 1.125 + new SourceNode(1, 0, "b.js", "function b() {\n"), 1.126 + new SourceNode(2, 0, "b.js", " var ret;\n"), 1.127 + new SourceNode(3, 0, "b.js", " // Empty line\n"), 1.128 + new SourceNode(4, 0, "b.js", " ret = 'b';\n"), 1.129 + new SourceNode(5, 0, "b.js", " return ret;\n"), 1.130 + new SourceNode(6, 0, "b.js", "}\n") 1.131 + ]); 1.132 + let c = new SourceNode(null, null, null, [ 1.133 + new SourceNode(1, 0, "c.js", "function c() {\n"), 1.134 + new SourceNode(2, 0, "c.js", " var ret;\n"), 1.135 + new SourceNode(3, 0, "c.js", " // Empty line\n"), 1.136 + new SourceNode(4, 0, "c.js", " ret = 'c';\n"), 1.137 + new SourceNode(5, 0, "c.js", " return ret;\n"), 1.138 + new SourceNode(6, 0, "c.js", "}\n") 1.139 + ]); 1.140 + 1.141 + let { code, map } = (new SourceNode(null, null, null, [ 1.142 + a, b, c 1.143 + ])).toStringWithSourceMap({ 1.144 + file: "http://example.com/www/js/abc.js", 1.145 + sourceRoot: "http://example.com/www/js/" 1.146 + }); 1.147 + 1.148 + code += "//# sourceMappingURL=data:text/json;base64," + btoa(map.toString()); 1.149 + 1.150 + Components.utils.evalInSandbox(code, gDebuggee, "1.8", 1.151 + "http://example.com/www/js/abc.js", 1); 1.152 +}