browser_scratchpad_contexts.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. "use strict";
  4. function test() {
  5. waitForExplicitFinish();
  6. gBrowser.selectedTab = gBrowser.addTab();
  7. gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
  8. gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
  9. openScratchpad(runTests);
  10. }, true);
  11. content.location = "data:text/html,test context switch in Scratchpad";
  12. }
  13. function runTests() {
  14. let sp = gScratchpadWindow.Scratchpad;
  15. let contentMenu = gScratchpadWindow.document.getElementById("sp-menu-content");
  16. let chromeMenu = gScratchpadWindow.document.getElementById("sp-menu-browser");
  17. let notificationBox = sp.notificationBox;
  18. ok(contentMenu, "found #sp-menu-content");
  19. ok(chromeMenu, "found #sp-menu-browser");
  20. ok(notificationBox, "found Scratchpad.notificationBox");
  21. let tests = [{
  22. method: "run",
  23. prepare: function* () {
  24. sp.setContentContext();
  25. is(sp.executionContext, gScratchpadWindow.SCRATCHPAD_CONTEXT_CONTENT,
  26. "executionContext is content");
  27. is(contentMenu.getAttribute("checked"), "true",
  28. "content menuitem is checked");
  29. isnot(chromeMenu.getAttribute("checked"), "true",
  30. "chrome menuitem is not checked");
  31. ok(!notificationBox.currentNotification,
  32. "there is no notification in content context");
  33. sp.editor.setText("window.foobarBug636725 = 'aloha';");
  34. let pageResult = yield inContent(function* () {
  35. return content.wrappedJSObject.foobarBug636725;
  36. });
  37. ok(!pageResult, "no content.foobarBug636725");
  38. },
  39. then: function* () {
  40. is(content.wrappedJSObject.foobarBug636725, "aloha",
  41. "content.foobarBug636725 has been set");
  42. }
  43. }, {
  44. method: "run",
  45. prepare: function* () {
  46. sp.setBrowserContext();
  47. is(sp.executionContext, gScratchpadWindow.SCRATCHPAD_CONTEXT_BROWSER,
  48. "executionContext is chrome");
  49. is(chromeMenu.getAttribute("checked"), "true",
  50. "chrome menuitem is checked");
  51. isnot(contentMenu.getAttribute("checked"), "true",
  52. "content menuitem is not checked");
  53. ok(notificationBox.currentNotification,
  54. "there is a notification in browser context");
  55. let [ from, to ] = sp.editor.getPosition(31, 32);
  56. sp.editor.replaceText("2'", from, to);
  57. is(sp.getText(), "window.foobarBug636725 = 'aloha2';",
  58. "setText() worked");
  59. },
  60. then: function* () {
  61. is(window.foobarBug636725, "aloha2",
  62. "window.foobarBug636725 has been set");
  63. delete window.foobarBug636725;
  64. ok(!window.foobarBug636725, "no window.foobarBug636725");
  65. }
  66. }, {
  67. method: "run",
  68. prepare: function* () {
  69. sp.editor.replaceText("gBrowser", sp.editor.getPosition(7));
  70. is(sp.getText(), "window.gBrowser",
  71. "setText() worked with no end for the replace range");
  72. },
  73. then: function* ([, , result]) {
  74. is(result.class, "XULElement",
  75. "chrome context has access to chrome objects");
  76. }
  77. }, {
  78. method: "run",
  79. prepare: function* () {
  80. // Check that the sandbox is cached.
  81. sp.editor.setText("typeof foobarBug636725cache;");
  82. },
  83. then: function* ([, , result]) {
  84. is(result, "undefined", "global variable does not exist");
  85. }
  86. }, {
  87. method: "run",
  88. prepare: function* () {
  89. sp.editor.setText("window.foobarBug636725cache = 'foo';" +
  90. "typeof foobarBug636725cache;");
  91. },
  92. then: function* ([, , result]) {
  93. is(result, "string",
  94. "global variable exists across two different executions");
  95. }
  96. }, {
  97. method: "run",
  98. prepare: function* () {
  99. sp.editor.setText("window.foobarBug636725cache2 = 'foo';" +
  100. "typeof foobarBug636725cache2;");
  101. },
  102. then: function* ([, , result]) {
  103. is(result, "string",
  104. "global variable exists across two different executions");
  105. }
  106. }, {
  107. method: "run",
  108. prepare: function* () {
  109. sp.setContentContext();
  110. is(sp.executionContext, gScratchpadWindow.SCRATCHPAD_CONTEXT_CONTENT,
  111. "executionContext is content");
  112. sp.editor.setText("typeof foobarBug636725cache2;");
  113. },
  114. then: function* ([, , result]) {
  115. is(result, "undefined",
  116. "global variable no longer exists after changing the context");
  117. }
  118. }];
  119. runAsyncCallbackTests(sp, tests).then(() => {
  120. sp.setBrowserContext();
  121. sp.editor.setText("delete foobarBug636725cache;" +
  122. "delete foobarBug636725cache2;");
  123. sp.run().then(finish);
  124. });
  125. }