test_sourcemaps-03.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. /**
  4. * Check setting breakpoints in source mapped sources.
  5. */
  6. var gDebuggee;
  7. var gClient;
  8. var gThreadClient;
  9. const {SourceNode} = require("source-map");
  10. function run_test()
  11. {
  12. initTestDebuggerServer();
  13. gDebuggee = addTestGlobal("test-source-map");
  14. gClient = new DebuggerClient(DebuggerServer.connectPipe());
  15. gClient.connect().then(function () {
  16. attachTestTabAndResume(gClient, "test-source-map", function (aResponse, aTabClient, aThreadClient) {
  17. gThreadClient = aThreadClient;
  18. test_simple_source_map();
  19. });
  20. });
  21. do_test_pending();
  22. }
  23. function testBreakpointMapping(aName, aCallback)
  24. {
  25. Task.spawn(function* () {
  26. let response = yield waitForPause(gThreadClient);
  27. do_check_eq(response.why.type, "debuggerStatement");
  28. const source = yield getSource(gThreadClient, "http://example.com/www/js/" + aName + ".js");
  29. response = yield setBreakpoint(source, {
  30. // Setting the breakpoint on an empty line so that it is pushed down one
  31. // line and we can check the source mapped actualLocation later.
  32. line: 3
  33. });
  34. // Should not slide breakpoints for sourcemapped sources
  35. do_check_true(!response.actualLocation);
  36. yield setBreakpoint(source, { line: 4 });
  37. // The eval will cause us to resume, then we get an unsolicited pause
  38. // because of our breakpoint, we resume again to finish the eval, and
  39. // finally receive our last pause which has the result of the client
  40. // evaluation.
  41. response = yield gThreadClient.eval(null, aName + "()");
  42. do_check_eq(response.type, "resumed");
  43. response = yield waitForPause(gThreadClient);
  44. do_check_eq(response.why.type, "breakpoint");
  45. // Assert that we paused because of the breakpoint at the correct
  46. // location in the code by testing that the value of `ret` is still
  47. // undefined.
  48. do_check_eq(response.frame.environment.bindings.variables.ret.value.type,
  49. "undefined");
  50. response = yield resume(gThreadClient);
  51. response = yield waitForPause(gThreadClient);
  52. do_check_eq(response.why.type, "clientEvaluated");
  53. do_check_eq(response.why.frameFinished.return, aName);
  54. response = yield resume(gThreadClient);
  55. aCallback();
  56. });
  57. gDebuggee.eval("(" + function () {
  58. debugger;
  59. } + "());");
  60. }
  61. function test_simple_source_map()
  62. {
  63. let expectedSources = new Set([
  64. "http://example.com/www/js/a.js",
  65. "http://example.com/www/js/b.js",
  66. "http://example.com/www/js/c.js"
  67. ]);
  68. gThreadClient.addListener("newSource", function _onNewSource(aEvent, aPacket) {
  69. expectedSources.delete(aPacket.source.url);
  70. if (expectedSources.size > 0) {
  71. return;
  72. }
  73. gThreadClient.removeListener("newSource", _onNewSource);
  74. testBreakpointMapping("a", function () {
  75. testBreakpointMapping("b", function () {
  76. testBreakpointMapping("c", function () {
  77. finishClient(gClient);
  78. });
  79. });
  80. });
  81. });
  82. let a = new SourceNode(null, null, null, [
  83. new SourceNode(1, 0, "a.js", "function a() {\n"),
  84. new SourceNode(2, 0, "a.js", " var ret;\n"),
  85. new SourceNode(3, 0, "a.js", " // Empty line\n"),
  86. new SourceNode(4, 0, "a.js", " ret = 'a';\n"),
  87. new SourceNode(5, 0, "a.js", " return ret;\n"),
  88. new SourceNode(6, 0, "a.js", "}\n")
  89. ]);
  90. let b = new SourceNode(null, null, null, [
  91. new SourceNode(1, 0, "b.js", "function b() {\n"),
  92. new SourceNode(2, 0, "b.js", " var ret;\n"),
  93. new SourceNode(3, 0, "b.js", " // Empty line\n"),
  94. new SourceNode(4, 0, "b.js", " ret = 'b';\n"),
  95. new SourceNode(5, 0, "b.js", " return ret;\n"),
  96. new SourceNode(6, 0, "b.js", "}\n")
  97. ]);
  98. let c = new SourceNode(null, null, null, [
  99. new SourceNode(1, 0, "c.js", "function c() {\n"),
  100. new SourceNode(2, 0, "c.js", " var ret;\n"),
  101. new SourceNode(3, 0, "c.js", " // Empty line\n"),
  102. new SourceNode(4, 0, "c.js", " ret = 'c';\n"),
  103. new SourceNode(5, 0, "c.js", " return ret;\n"),
  104. new SourceNode(6, 0, "c.js", "}\n")
  105. ]);
  106. let { code, map } = (new SourceNode(null, null, null, [
  107. a, b, c
  108. ])).toStringWithSourceMap({
  109. file: "http://example.com/www/js/abc.js",
  110. sourceRoot: "http://example.com/www/js/"
  111. });
  112. code += "//# sourceMappingURL=data:text/json;base64," + btoa(map.toString());
  113. Components.utils.evalInSandbox(code, gDebuggee, "1.8",
  114. "http://example.com/www/js/abc.js", 1);
  115. }