michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: function test() michael@0: { michael@0: waitForExplicitFinish(); michael@0: michael@0: gBrowser.selectedTab = gBrowser.addTab(); michael@0: gBrowser.selectedBrowser.addEventListener("load", function onLoad() { michael@0: gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); michael@0: openScratchpad(runTests); michael@0: }, true); michael@0: michael@0: content.location = "data:text/html,test context switch in Scratchpad"; michael@0: } michael@0: michael@0: michael@0: function runTests() michael@0: { michael@0: let sp = gScratchpadWindow.Scratchpad; michael@0: let contentMenu = gScratchpadWindow.document.getElementById("sp-menu-content"); michael@0: let chromeMenu = gScratchpadWindow.document.getElementById("sp-menu-browser"); michael@0: let notificationBox = sp.notificationBox; michael@0: michael@0: ok(contentMenu, "found #sp-menu-content"); michael@0: ok(chromeMenu, "found #sp-menu-browser"); michael@0: ok(notificationBox, "found Scratchpad.notificationBox"); michael@0: michael@0: let tests = [{ michael@0: method: "run", michael@0: prepare: function() { michael@0: sp.setContentContext(); michael@0: michael@0: is(sp.executionContext, gScratchpadWindow.SCRATCHPAD_CONTEXT_CONTENT, michael@0: "executionContext is content"); michael@0: michael@0: is(contentMenu.getAttribute("checked"), "true", michael@0: "content menuitem is checked"); michael@0: michael@0: isnot(chromeMenu.getAttribute("checked"), "true", michael@0: "chrome menuitem is not checked"); michael@0: michael@0: ok(!notificationBox.currentNotification, michael@0: "there is no notification in content context"); michael@0: michael@0: sp.editor.setText("window.foobarBug636725 = 'aloha';"); michael@0: michael@0: ok(!content.wrappedJSObject.foobarBug636725, michael@0: "no content.foobarBug636725"); michael@0: }, michael@0: then: function() { michael@0: is(content.wrappedJSObject.foobarBug636725, "aloha", michael@0: "content.foobarBug636725 has been set"); michael@0: } michael@0: }, michael@0: { michael@0: method: "run", michael@0: prepare: function() { michael@0: sp.setBrowserContext(); michael@0: michael@0: is(sp.executionContext, gScratchpadWindow.SCRATCHPAD_CONTEXT_BROWSER, michael@0: "executionContext is chrome"); michael@0: michael@0: is(chromeMenu.getAttribute("checked"), "true", michael@0: "chrome menuitem is checked"); michael@0: michael@0: isnot(contentMenu.getAttribute("checked"), "true", michael@0: "content menuitem is not checked"); michael@0: michael@0: ok(notificationBox.currentNotification, michael@0: "there is a notification in browser context"); michael@0: michael@0: let [ from, to ] = sp.editor.getPosition(31, 32); michael@0: sp.editor.replaceText("2'", from, to); michael@0: michael@0: is(sp.getText(), "window.foobarBug636725 = 'aloha2';", michael@0: "setText() worked"); michael@0: }, michael@0: then: function() { michael@0: is(window.foobarBug636725, "aloha2", michael@0: "window.foobarBug636725 has been set"); michael@0: michael@0: delete window.foobarBug636725; michael@0: ok(!window.foobarBug636725, "no window.foobarBug636725"); michael@0: } michael@0: }, michael@0: { michael@0: method: "run", michael@0: prepare: function() { michael@0: sp.editor.replaceText("gBrowser", sp.editor.getPosition(7)); michael@0: michael@0: is(sp.getText(), "window.gBrowser", michael@0: "setText() worked with no end for the replace range"); michael@0: }, michael@0: then: function([, , result]) { michael@0: is(result.class, "XULElement", michael@0: "chrome context has access to chrome objects"); michael@0: } michael@0: }, michael@0: { michael@0: method: "run", michael@0: prepare: function() { michael@0: // Check that the sandbox is cached. michael@0: sp.editor.setText("typeof foobarBug636725cache;"); michael@0: }, michael@0: then: function([, , result]) { michael@0: is(result, "undefined", "global variable does not exist"); michael@0: } michael@0: }, michael@0: { michael@0: method: "run", michael@0: prepare: function() { michael@0: sp.editor.setText("var foobarBug636725cache = 'foo';" + michael@0: "typeof foobarBug636725cache;"); michael@0: }, michael@0: then: function([, , result]) { michael@0: is(result, "string", michael@0: "global variable exists across two different executions"); michael@0: } michael@0: }, michael@0: { michael@0: method: "run", michael@0: prepare: function() { michael@0: sp.editor.setText("var foobarBug636725cache2 = 'foo';" + michael@0: "typeof foobarBug636725cache2;"); michael@0: }, michael@0: then: function([, , result]) { michael@0: is(result, "string", michael@0: "global variable exists across two different executions"); michael@0: } michael@0: }, michael@0: { michael@0: method: "run", michael@0: prepare: function() { michael@0: sp.setContentContext(); michael@0: michael@0: is(sp.executionContext, gScratchpadWindow.SCRATCHPAD_CONTEXT_CONTENT, michael@0: "executionContext is content"); michael@0: michael@0: sp.editor.setText("typeof foobarBug636725cache2;"); michael@0: }, michael@0: then: function([, , result]) { michael@0: is(result, "undefined", michael@0: "global variable no longer exists after changing the context"); michael@0: } michael@0: }]; michael@0: michael@0: runAsyncCallbackTests(sp, tests).then(() => { michael@0: sp.setBrowserContext(); michael@0: sp.editor.setText("delete foobarBug636725cache;" + michael@0: "delete foobarBug636725cache2;"); michael@0: sp.run().then(finish); michael@0: }); michael@0: }