diff --git a/browser/base/content/scratchpad.js b/browser/base/content/scratchpad.js index 8defb62b5c83..04962b61dfbb 100644 --- a/browser/base/content/scratchpad.js +++ b/browser/base/content/scratchpad.js @@ -129,8 +129,9 @@ var Scratchpad = { /** * Get the Cu.Sandbox object for the active tab content window object. Note * that the returned object is cached for later reuse. The cached object is - * kept only for the current browser window and it is reset for each context - * switch or navigator:browser window switch. + * kept only for the current location in the current tab of the current + * browser window and it is reset for each context switch, + * navigator:browser window switch, tab switch or navigation. */ get contentSandbox() { @@ -141,12 +142,16 @@ var Scratchpad = { } if (!this._contentSandbox || - this.browserWindow != this._previousBrowserWindow) { + this.browserWindow != this._previousBrowserWindow || + this._previousBrowser != this.gBrowser.selectedBrowser || + this._previousLocation != this.gBrowser.contentWindow.location.href) { let contentWindow = this.gBrowser.selectedBrowser.contentWindow; this._contentSandbox = new Cu.Sandbox(contentWindow, { sandboxPrototype: contentWindow, wantXrays: false }); this._previousBrowserWindow = this.browserWindow; + this._previousBrowser = this.gBrowser.selectedBrowser; + this._previousLocation = contentWindow.location.href; } return this._contentSandbox; @@ -558,8 +563,8 @@ var Scratchpad = { let content = document.getElementById("sp-menu-content"); document.getElementById("sp-menu-browser").removeAttribute("checked"); content.setAttribute("checked", true); - this.statusbarStatus.label = content.getAttribute("label"); this.executionContext = SCRATCHPAD_CONTEXT_CONTENT; + this.statusbarStatus.label = content.getAttribute("label"); this.resetContext(); }, @@ -568,11 +573,11 @@ var Scratchpad = { */ setBrowserContext: function SP_setBrowserContext() { - let chrome = document.getElementById("sp-menu-browser"); + let browser = document.getElementById("sp-menu-browser"); document.getElementById("sp-menu-content").removeAttribute("checked"); - chrome.setAttribute("checked", true); - this.statusbarStatus.label = chrome.getAttribute("label"); + browser.setAttribute("checked", true); this.executionContext = SCRATCHPAD_CONTEXT_BROWSER; + this.statusbarStatus.label = browser.getAttribute("label"); this.resetContext(); }, @@ -584,6 +589,8 @@ var Scratchpad = { this._chromeSandbox = null; this._contentSandbox = null; this._previousWindow = null; + this._previousBrowser = null; + this._previousLocation = null; }, /** diff --git a/browser/base/content/test/Makefile.in b/browser/base/content/test/Makefile.in index e8bc6f32657d..5b3f03ab1122 100644 --- a/browser/base/content/test/Makefile.in +++ b/browser/base/content/test/Makefile.in @@ -194,6 +194,7 @@ _BROWSER_FILES = \ browser_inspector_treePanel_result.html \ browser_scratchpad_initialization.js \ browser_scratchpad_contexts.js \ + browser_scratchpad_tab_switch.js \ browser_scratchpad_execute_print.js \ browser_scratchpad_inspect.js \ browser_scratchpad_files.js \ diff --git a/browser/base/content/test/browser_scratchpad_tab_switch.js b/browser/base/content/test/browser_scratchpad_tab_switch.js new file mode 100644 index 000000000000..11d3d4faefd3 --- /dev/null +++ b/browser/base/content/test/browser_scratchpad_tab_switch.js @@ -0,0 +1,111 @@ +/* vim: set ts=2 et sw=2 tw=80: */ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +// Reference to the Scratchpad chrome window object. +let gScratchpadWindow; +let tab1; +let tab2; +let sp; + +function test() +{ + waitForExplicitFinish(); + + tab1 = gBrowser.addTab(); + gBrowser.selectedTab = tab1; + gBrowser.selectedBrowser.addEventListener("load", function() { + gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true); + + tab2 = gBrowser.addTab(); + gBrowser.selectedTab = tab2; + gBrowser.selectedBrowser.addEventListener("load", function() { + gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true); + gScratchpadWindow = Scratchpad.openScratchpad(); + gScratchpadWindow.addEventListener("load", runTests, false); + }, true); + content.location = "data:text/html,test context switch in Scratchpad tab 2"; + }, true); + + content.location = "data:text/html,test context switch in Scratchpad tab 1"; +} + +function runTests() +{ + gScratchpadWindow.removeEventListener("load", runTests, true); + + sp = gScratchpadWindow.Scratchpad; + + let contentMenu = gScratchpadWindow.document.getElementById("sp-menu-content"); + let browserMenu = gScratchpadWindow.document.getElementById("sp-menu-browser"); + let statusbar = sp.statusbarStatus; + + ok(contentMenu, "found #sp-menu-content"); + ok(browserMenu, "found #sp-menu-browser"); + ok(statusbar, "found Scratchpad.statusbarStatus"); + + sp.setContentContext(); + + is(sp.executionContext, gScratchpadWindow.SCRATCHPAD_CONTEXT_CONTENT, + "executionContext is content"); + + is(contentMenu.getAttribute("checked"), "true", + "content menuitem is checked"); + + ok(!browserMenu.hasAttribute("checked"), + "chrome menuitem is not checked"); + + is(statusbar.getAttribute("label"), contentMenu.getAttribute("label"), + "statusbar label is correct"); + + ok(sp.textbox, "textbox exists"); + sp.textbox.value = "window.foosbug653108 = 'aloha';"; + + ok(!content.wrappedJSObject.foosbug653108, + "no content.foosbug653108"); + + sp.run(); + + is(content.wrappedJSObject.foosbug653108, "aloha", + "content.foosbug653108 has been set"); + + gBrowser.tabContainer.addEventListener("TabSelect", runTests2, true); + gBrowser.selectedTab = tab1; +} + +function runTests2() { + gBrowser.tabContainer.removeEventListener("TabSelect", runTests2, true); + + ok(!window.foosbug653108, "no window.foosbug653108"); + + sp.textbox.value = "window.foosbug653108"; + let result = sp.run(); + + isnot(result, "aloha", "window.foosbug653108 is not aloha"); + + sp.textbox.value = "window.foosbug653108 = 'ahoyhoy';"; + sp.run(); + + is(content.wrappedJSObject.foosbug653108, "ahoyhoy", + "content.foosbug653108 has been set 2"); + + gBrowser.selectedBrowser.addEventListener("load", runTests3, true); + content.location = "data:text/html,test context switch in Scratchpad location 2"; +} + +function runTests3() { + gBrowser.selectedBrowser.removeEventListener("load", runTests3, true); + // Check that the sandbox is not cached. + + sp.textbox.value = "typeof foosbug653108;"; + is(sp.run()[1], "undefined", "global variable does not exist"); + + gScratchpadWindow.close(); + gScratchpadWindow = null; + tab1 = null; + tab2 = null; + sp = null; + gBrowser.removeCurrentTab(); + gBrowser.removeCurrentTab(); + finish(); +} diff --git a/browser/locales/en-US/chrome/browser/scratchpad.properties b/browser/locales/en-US/chrome/browser/scratchpad.properties index 916a2dcc82ae..01e0bf4988e7 100644 --- a/browser/locales/en-US/chrome/browser/scratchpad.properties +++ b/browser/locales/en-US/chrome/browser/scratchpad.properties @@ -28,3 +28,4 @@ saveFileAs=Save File As # LOCALIZATION NOTE (saveFile.failed): This is the message displayed when file # save fails. saveFile.failed=The file save operation failed. +