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: const BUTTON_POSITION_CANCEL = 1; michael@0: const BUTTON_POSITION_DONT_SAVE = 2; michael@0: michael@0: michael@0: function test() michael@0: { michael@0: waitForExplicitFinish(); michael@0: michael@0: // Observer must be attached *before* Scratchpad is opened. michael@0: CloseObserver.init(); michael@0: michael@0: gBrowser.selectedTab = gBrowser.addTab(); michael@0: gBrowser.selectedBrowser.addEventListener("load", function onTabLoad() { michael@0: gBrowser.selectedBrowser.removeEventListener("load", onTabLoad, true); michael@0: openScratchpad(runTests); michael@0: }, true); michael@0: michael@0: content.location = "data:text/html;charset=utf8,

test browser last window closing

"; michael@0: } michael@0: michael@0: michael@0: michael@0: function runTests({ Scratchpad }) michael@0: { michael@0: let browser = Services.wm.getEnumerator("navigator:browser").getNext(); michael@0: let oldPrompt = Services.prompt; michael@0: let button; michael@0: michael@0: Services.prompt = { michael@0: confirmEx: () => button michael@0: }; michael@0: michael@0: michael@0: Scratchpad.dirty = true; michael@0: michael@0: // Test canceling close. michael@0: button = BUTTON_POSITION_CANCEL; michael@0: CloseObserver.expectedValue = true; michael@0: browser.BrowserTryToCloseWindow(); michael@0: michael@0: // Test accepting close. michael@0: button = BUTTON_POSITION_DONT_SAVE; michael@0: CloseObserver.expectedValue = false; michael@0: browser.BrowserTryToCloseWindow(); michael@0: michael@0: // Test closing without prompt needed. michael@0: Scratchpad.dirty = false; michael@0: browser.BrowserTryToCloseWindow(); michael@0: michael@0: Services.prompt = oldPrompt; michael@0: CloseObserver.uninit(); michael@0: finish(); michael@0: } michael@0: michael@0: michael@0: let CloseObserver = { michael@0: expectedValue: null, michael@0: init: function() michael@0: { michael@0: Services.obs.addObserver(this, "browser-lastwindow-close-requested", false); michael@0: }, michael@0: michael@0: observe: function(aSubject) michael@0: { michael@0: aSubject.QueryInterface(Ci.nsISupportsPRBool); michael@0: let message = this.expectedValue ? "close" : "stay open"; michael@0: ok(this.expectedValue === aSubject.data, "Expected browser to " + message); michael@0: aSubject.data = true; michael@0: }, michael@0: michael@0: uninit: function() michael@0: { michael@0: Services.obs.removeObserver(this, "browser-lastwindow-close-requested", false); michael@0: }, michael@0: };