browser_source_map-02.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. /**
  4. * Tests the SourceMapService updates generated sources when pretty printing
  5. * and un pretty printing.
  6. */
  7. // Force the old debugger UI since it's directly used (see Bug 1301705)
  8. Services.prefs.setBoolPref("devtools.debugger.new-debugger-frontend", false);
  9. registerCleanupFunction(function* () {
  10. Services.prefs.clearUserPref("devtools.debugger.new-debugger-frontend");
  11. });
  12. const DEBUGGER_ROOT = "http://example.com/browser/devtools/client/debugger/test/mochitest/";
  13. // Empty page
  14. const PAGE_URL = `${DEBUGGER_ROOT}doc_empty-tab-01.html`;
  15. const JS_URL = `${URL_ROOT}code_ugly.js`;
  16. const { SourceMapService } = require("devtools/client/framework/source-map-service");
  17. add_task(function* () {
  18. let toolbox = yield openNewTabAndToolbox(PAGE_URL, "jsdebugger");
  19. let service = new SourceMapService(toolbox.target);
  20. let checkedPretty = false;
  21. let checkedUnpretty = false;
  22. function onUpdate(e, oldLoc, newLoc) {
  23. if (oldLoc.line === 3) {
  24. checkPrettified(oldLoc, newLoc);
  25. checkedPretty = true;
  26. } else if (oldLoc.line === 9) {
  27. checkUnprettified(oldLoc, newLoc);
  28. checkedUnpretty = true;
  29. } else {
  30. throw new Error(`Unexpected location update: ${JSON.stringify(oldLoc)}`);
  31. }
  32. }
  33. const loc1 = { url: JS_URL, line: 3 };
  34. service.subscribe(loc1, onUpdate);
  35. // Inject JS script
  36. let sourceShown = waitForSourceShown(toolbox.getCurrentPanel(), "code_ugly.js");
  37. yield createScript(JS_URL);
  38. yield sourceShown;
  39. let ppButton = toolbox.getCurrentPanel().panelWin.document.getElementById("pretty-print");
  40. sourceShown = waitForSourceShown(toolbox.getCurrentPanel(), "code_ugly.js");
  41. ppButton.click();
  42. yield sourceShown;
  43. yield waitUntil(() => checkedPretty);
  44. // TODO check unprettified change once bug 1177446 fixed
  45. // info("Testing un-pretty printing.");
  46. // sourceShown = waitForSourceShown(toolbox.getCurrentPanel(), "code_ugly.js");
  47. // ppButton.click();
  48. // yield sourceShown;
  49. // yield waitUntil(() => checkedUnpretty);
  50. yield toolbox.destroy();
  51. gBrowser.removeCurrentTab();
  52. finish();
  53. });
  54. function checkPrettified(oldLoc, newLoc) {
  55. is(oldLoc.line, 3, "Correct line for JS:3");
  56. is(oldLoc.column, null, "Correct column for JS:3");
  57. is(oldLoc.url, JS_URL, "Correct url for JS:3");
  58. is(newLoc.line, 9, "Correct line for JS:3 -> PRETTY");
  59. is(newLoc.column, 0, "Correct column for JS:3 -> PRETTY");
  60. is(newLoc.url, JS_URL, "Correct url for JS:3 -> PRETTY");
  61. }
  62. function checkUnprettified(oldLoc, newLoc) {
  63. is(oldLoc.line, 9, "Correct line for JS:3 -> PRETTY");
  64. is(oldLoc.column, 0, "Correct column for JS:3 -> PRETTY");
  65. is(oldLoc.url, JS_URL, "Correct url for JS:3 -> PRETTY");
  66. is(newLoc.line, 3, "Correct line for JS:3 -> UNPRETTIED");
  67. is(newLoc.column, null, "Correct column for JS:3 -> UNPRETTIED");
  68. is(newLoc.url, JS_URL, "Correct url for JS:3 -> UNPRETTIED");
  69. }
  70. function createScript(url) {
  71. info(`Creating script: ${url}`);
  72. let mm = getFrameScript();
  73. let command = `
  74. let script = document.createElement("script");
  75. script.setAttribute("src", "${url}");
  76. document.body.appendChild(script);
  77. `;
  78. return evalInDebuggee(mm, command);
  79. }
  80. function waitForSourceShown(debuggerPanel, url) {
  81. let { panelWin } = debuggerPanel;
  82. let deferred = defer();
  83. info(`Waiting for source ${url} to be shown in the debugger...`);
  84. panelWin.on(panelWin.EVENTS.SOURCE_SHOWN, function onSourceShown(_, source) {
  85. let sourceUrl = source.url || source.introductionUrl;
  86. if (sourceUrl.includes(url)) {
  87. panelWin.off(panelWin.EVENTS.SOURCE_SHOWN, onSourceShown);
  88. info(`Source shown for ${url}`);
  89. deferred.resolve(source);
  90. }
  91. });
  92. return deferred.promise;
  93. }