browser/devtools/scratchpad/test/browser_scratchpad_contexts.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial