browser/devtools/scratchpad/test/browser_scratchpad_contexts.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/scratchpad/test/browser_scratchpad_contexts.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,155 @@
     1.4 +/* vim: set ts=2 et sw=2 tw=80: */
     1.5 +/* Any copyright is dedicated to the Public Domain.
     1.6 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.7 +
     1.8 +function test()
     1.9 +{
    1.10 +  waitForExplicitFinish();
    1.11 +
    1.12 +  gBrowser.selectedTab = gBrowser.addTab();
    1.13 +  gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
    1.14 +    gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
    1.15 +    openScratchpad(runTests);
    1.16 +  }, true);
    1.17 +
    1.18 +  content.location = "data:text/html,test context switch in Scratchpad";
    1.19 +}
    1.20 +
    1.21 +
    1.22 +function runTests()
    1.23 +{
    1.24 +  let sp = gScratchpadWindow.Scratchpad;
    1.25 +  let contentMenu = gScratchpadWindow.document.getElementById("sp-menu-content");
    1.26 +  let chromeMenu = gScratchpadWindow.document.getElementById("sp-menu-browser");
    1.27 +  let notificationBox = sp.notificationBox;
    1.28 +
    1.29 +  ok(contentMenu, "found #sp-menu-content");
    1.30 +  ok(chromeMenu, "found #sp-menu-browser");
    1.31 +  ok(notificationBox, "found Scratchpad.notificationBox");
    1.32 +
    1.33 +  let tests = [{
    1.34 +    method: "run",
    1.35 +    prepare: function() {
    1.36 +      sp.setContentContext();
    1.37 +
    1.38 +      is(sp.executionContext, gScratchpadWindow.SCRATCHPAD_CONTEXT_CONTENT,
    1.39 +         "executionContext is content");
    1.40 +
    1.41 +      is(contentMenu.getAttribute("checked"), "true",
    1.42 +         "content menuitem is checked");
    1.43 +
    1.44 +      isnot(chromeMenu.getAttribute("checked"), "true",
    1.45 +         "chrome menuitem is not checked");
    1.46 +
    1.47 +      ok(!notificationBox.currentNotification,
    1.48 +         "there is no notification in content context");
    1.49 +
    1.50 +      sp.editor.setText("window.foobarBug636725 = 'aloha';");
    1.51 +
    1.52 +      ok(!content.wrappedJSObject.foobarBug636725,
    1.53 +         "no content.foobarBug636725");
    1.54 +    },
    1.55 +    then: function() {
    1.56 +      is(content.wrappedJSObject.foobarBug636725, "aloha",
    1.57 +         "content.foobarBug636725 has been set");
    1.58 +    }
    1.59 +  },
    1.60 +  {
    1.61 +    method: "run",
    1.62 +    prepare: function() {
    1.63 +      sp.setBrowserContext();
    1.64 +
    1.65 +      is(sp.executionContext, gScratchpadWindow.SCRATCHPAD_CONTEXT_BROWSER,
    1.66 +         "executionContext is chrome");
    1.67 +
    1.68 +      is(chromeMenu.getAttribute("checked"), "true",
    1.69 +         "chrome menuitem is checked");
    1.70 +
    1.71 +      isnot(contentMenu.getAttribute("checked"), "true",
    1.72 +         "content menuitem is not checked");
    1.73 +
    1.74 +      ok(notificationBox.currentNotification,
    1.75 +         "there is a notification in browser context");
    1.76 +
    1.77 +      let [ from, to ] = sp.editor.getPosition(31, 32);
    1.78 +      sp.editor.replaceText("2'", from, to);
    1.79 +
    1.80 +      is(sp.getText(), "window.foobarBug636725 = 'aloha2';",
    1.81 +         "setText() worked");
    1.82 +    },
    1.83 +    then: function() {
    1.84 +      is(window.foobarBug636725, "aloha2",
    1.85 +         "window.foobarBug636725 has been set");
    1.86 +
    1.87 +      delete window.foobarBug636725;
    1.88 +      ok(!window.foobarBug636725, "no window.foobarBug636725");
    1.89 +    }
    1.90 +  },
    1.91 +  {
    1.92 +    method: "run",
    1.93 +    prepare: function() {
    1.94 +      sp.editor.replaceText("gBrowser", sp.editor.getPosition(7));
    1.95 +
    1.96 +      is(sp.getText(), "window.gBrowser",
    1.97 +         "setText() worked with no end for the replace range");
    1.98 +    },
    1.99 +    then: function([, , result]) {
   1.100 +      is(result.class, "XULElement",
   1.101 +         "chrome context has access to chrome objects");
   1.102 +    }
   1.103 +  },
   1.104 +  {
   1.105 +    method: "run",
   1.106 +    prepare: function() {
   1.107 +      // Check that the sandbox is cached.
   1.108 +      sp.editor.setText("typeof foobarBug636725cache;");
   1.109 +    },
   1.110 +    then: function([, , result]) {
   1.111 +      is(result, "undefined", "global variable does not exist");
   1.112 +    }
   1.113 +  },
   1.114 +  {
   1.115 +    method: "run",
   1.116 +    prepare: function() {
   1.117 +      sp.editor.setText("var foobarBug636725cache = 'foo';" +
   1.118 +                 "typeof foobarBug636725cache;");
   1.119 +    },
   1.120 +    then: function([, , result]) {
   1.121 +      is(result, "string",
   1.122 +         "global variable exists across two different executions");
   1.123 +    }
   1.124 +  },
   1.125 +  {
   1.126 +    method: "run",
   1.127 +    prepare: function() {
   1.128 +      sp.editor.setText("var foobarBug636725cache2 = 'foo';" +
   1.129 +                 "typeof foobarBug636725cache2;");
   1.130 +    },
   1.131 +    then: function([, , result]) {
   1.132 +      is(result, "string",
   1.133 +         "global variable exists across two different executions");
   1.134 +    }
   1.135 +  },
   1.136 +  {
   1.137 +    method: "run",
   1.138 +    prepare: function() {
   1.139 +      sp.setContentContext();
   1.140 +
   1.141 +      is(sp.executionContext, gScratchpadWindow.SCRATCHPAD_CONTEXT_CONTENT,
   1.142 +         "executionContext is content");
   1.143 +
   1.144 +      sp.editor.setText("typeof foobarBug636725cache2;");
   1.145 +    },
   1.146 +    then: function([, , result]) {
   1.147 +      is(result, "undefined",
   1.148 +         "global variable no longer exists after changing the context");
   1.149 +    }
   1.150 +  }];
   1.151 +
   1.152 +  runAsyncCallbackTests(sp, tests).then(() => {
   1.153 +    sp.setBrowserContext();
   1.154 +    sp.editor.setText("delete foobarBug636725cache;" +
   1.155 +               "delete foobarBug636725cache2;");
   1.156 +    sp.run().then(finish);
   1.157 +  });
   1.158 +}
   1.159 \ No newline at end of file

mercurial