browser_cmd_jsb.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Any copyright is dedicated to the Public Domain.
  2. * http://creativecommons.org/publicdomain/zero/1.0/ */
  3. // Tests that the jsb command works as it should
  4. const TEST_URI = "http://example.com/browser/devtools/client/commandline/" +
  5. "test/browser_cmd_jsb_script.jsi";
  6. function test() {
  7. return Task.spawn(testTask).then(finish, helpers.handleError);
  8. }
  9. function* testTask() {
  10. let options = yield helpers.openTab("about:blank");
  11. yield helpers.openToolbar(options);
  12. let notifyPromise = wwNotifyOnce();
  13. helpers.audit(options, [
  14. {
  15. setup: "jsb",
  16. check: {
  17. input: "jsb",
  18. hints: " <url> [options]",
  19. markup: "VVV",
  20. status: "ERROR"
  21. }
  22. },
  23. {
  24. setup: "jsb " + TEST_URI,
  25. // Should result in a new scratchpad window
  26. exec: {
  27. output: "",
  28. error: false
  29. }
  30. }
  31. ]);
  32. let { subject } = yield notifyPromise;
  33. let scratchpadWin = subject.QueryInterface(Ci.nsIDOMWindow);
  34. yield helpers.listenOnce(scratchpadWin, "load");
  35. let scratchpad = scratchpadWin.Scratchpad;
  36. yield observeOnce(scratchpad);
  37. let result = scratchpad.getText();
  38. result = result.replace(/[\r\n]]*/g, "\n");
  39. let correct = "function somefunc() {\n" +
  40. " if (true) // Some comment\n" +
  41. " doSomething();\n" +
  42. " for (let n = 0; n < 500; n++) {\n" +
  43. " if (n % 2 == 1) {\n" +
  44. " console.log(n);\n" +
  45. " console.log(n + 1);\n" +
  46. " }\n" +
  47. " }\n" +
  48. "}";
  49. is(result, correct, "JS has been correctly prettified");
  50. if (scratchpadWin) {
  51. scratchpadWin.close();
  52. scratchpadWin = null;
  53. }
  54. yield helpers.closeToolbar(options);
  55. yield helpers.closeTab(options);
  56. }
  57. /**
  58. * A wrapper for calling Services.ww.[un]registerNotification using promises.
  59. * @return a promise that resolves when the notification service first notifies
  60. * with topic == "domwindowopened".
  61. * The value of the promise is { subject: subject, topic: topic, data: data }
  62. */
  63. function wwNotifyOnce() {
  64. return new Promise(resolve => {
  65. let onNotify = (subject, topic, data) => {
  66. if (topic == "domwindowopened") {
  67. Services.ww.unregisterNotification(onNotify);
  68. resolve({ subject: subject, topic: topic, data: data });
  69. }
  70. };
  71. Services.ww.registerNotification(onNotify);
  72. });
  73. }
  74. /**
  75. * YET ANOTHER WRAPPER for a place where we are using events as poor-man's
  76. * promises. Perhaps this should be promoted to scratchpad?
  77. */
  78. function observeOnce(scratchpad) {
  79. return new Promise(resolve => {
  80. let observer = {
  81. onReady: function () {
  82. scratchpad.removeObserver(observer);
  83. resolve();
  84. },
  85. };
  86. scratchpad.addObserver(observer);
  87. });
  88. }