1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/devtools/server/tests/unit/test_sourcemaps-09.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,96 @@ 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 that source maps and breakpoints work with minified javascript. 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_minified(); 1.26 + }); 1.27 + }); 1.28 + do_test_pending(); 1.29 +} 1.30 + 1.31 +function test_minified() 1.32 +{ 1.33 + let newSourceFired = false; 1.34 + 1.35 + gClient.addOneTimeListener("newSource", function _onNewSource(aEvent, aPacket) { 1.36 + do_check_eq(aEvent, "newSource"); 1.37 + do_check_eq(aPacket.type, "newSource"); 1.38 + do_check_true(!!aPacket.source); 1.39 + 1.40 + do_check_eq(aPacket.source.url, "http://example.com/foo.js", 1.41 + "The new source should be foo.js"); 1.42 + do_check_eq(aPacket.source.url.indexOf("foo.min.js"), -1, 1.43 + "The new source should not be the minified file"); 1.44 + 1.45 + newSourceFired = true; 1.46 + }); 1.47 + 1.48 + gThreadClient.addOneTimeListener("paused", function (aEvent, aPacket) { 1.49 + do_check_eq(aEvent, "paused"); 1.50 + do_check_eq(aPacket.why.type, "debuggerStatement"); 1.51 + 1.52 + const location = { 1.53 + url: "http://example.com/foo.js", 1.54 + line: 5 1.55 + }; 1.56 + 1.57 + gThreadClient.setBreakpoint(location, function (aResponse, bpClient) { 1.58 + do_check_true(!aResponse.error); 1.59 + testHitBreakpoint(); 1.60 + }); 1.61 + }); 1.62 + 1.63 + // This is the original foo.js, which was then minified with uglifyjs version 1.64 + // 2.2.5 and the "--mangle" option. 1.65 + // 1.66 + // (function () { 1.67 + // debugger; 1.68 + // function foo(n) { 1.69 + // var bar = n + n; 1.70 + // var unused = null; 1.71 + // return bar; 1.72 + // } 1.73 + // for (var i = 0; i < 10; i++) { 1.74 + // foo(i); 1.75 + // } 1.76 + // }()); 1.77 + 1.78 + let code = '(function(){debugger;function r(r){var n=r+r;var u=null;return n}for(var n=0;n<10;n++){r(n)}})();\n//# sourceMappingURL=data:text/json,{"file":"foo.min.js","version":3,"sources":["foo.js"],"names":["foo","n","bar","unused","i"],"mappings":"CAAC,WACC,QACA,SAASA,GAAIC,GACX,GAAIC,GAAMD,EAAIA,CACd,IAAIE,GAAS,IACb,OAAOD,GAET,IAAK,GAAIE,GAAI,EAAGA,EAAI,GAAIA,IAAK,CAC3BJ,EAAII"}'; 1.79 + 1.80 + Components.utils.evalInSandbox(code, gDebuggee, "1.8", 1.81 + "http://example.com/foo.min.js", 1); 1.82 +} 1.83 + 1.84 +function testHitBreakpoint(timesHit=0) { 1.85 + gClient.addOneTimeListener("paused", function (aEvent, aPacket) { 1.86 + ++timesHit; 1.87 + 1.88 + do_check_eq(aEvent, "paused"); 1.89 + do_check_eq(aPacket.why.type, "breakpoint"); 1.90 + 1.91 + if (timesHit === 10) { 1.92 + gThreadClient.resume(() => finishClient(gClient)); 1.93 + } else { 1.94 + testHitBreakpoint(timesHit); 1.95 + } 1.96 + }); 1.97 + 1.98 + gThreadClient.resume(); 1.99 +}