1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/scratchpad/test/browser_scratchpad_browser_last_window_closing.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,79 @@ 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 +const BUTTON_POSITION_CANCEL = 1; 1.9 +const BUTTON_POSITION_DONT_SAVE = 2; 1.10 + 1.11 + 1.12 +function test() 1.13 +{ 1.14 + waitForExplicitFinish(); 1.15 + 1.16 + // Observer must be attached *before* Scratchpad is opened. 1.17 + CloseObserver.init(); 1.18 + 1.19 + gBrowser.selectedTab = gBrowser.addTab(); 1.20 + gBrowser.selectedBrowser.addEventListener("load", function onTabLoad() { 1.21 + gBrowser.selectedBrowser.removeEventListener("load", onTabLoad, true); 1.22 + openScratchpad(runTests); 1.23 + }, true); 1.24 + 1.25 + content.location = "data:text/html;charset=utf8,<p>test browser last window closing</p>"; 1.26 +} 1.27 + 1.28 + 1.29 + 1.30 +function runTests({ Scratchpad }) 1.31 +{ 1.32 + let browser = Services.wm.getEnumerator("navigator:browser").getNext(); 1.33 + let oldPrompt = Services.prompt; 1.34 + let button; 1.35 + 1.36 + Services.prompt = { 1.37 + confirmEx: () => button 1.38 + }; 1.39 + 1.40 + 1.41 + Scratchpad.dirty = true; 1.42 + 1.43 + // Test canceling close. 1.44 + button = BUTTON_POSITION_CANCEL; 1.45 + CloseObserver.expectedValue = true; 1.46 + browser.BrowserTryToCloseWindow(); 1.47 + 1.48 + // Test accepting close. 1.49 + button = BUTTON_POSITION_DONT_SAVE; 1.50 + CloseObserver.expectedValue = false; 1.51 + browser.BrowserTryToCloseWindow(); 1.52 + 1.53 + // Test closing without prompt needed. 1.54 + Scratchpad.dirty = false; 1.55 + browser.BrowserTryToCloseWindow(); 1.56 + 1.57 + Services.prompt = oldPrompt; 1.58 + CloseObserver.uninit(); 1.59 + finish(); 1.60 +} 1.61 + 1.62 + 1.63 +let CloseObserver = { 1.64 + expectedValue: null, 1.65 + init: function() 1.66 + { 1.67 + Services.obs.addObserver(this, "browser-lastwindow-close-requested", false); 1.68 + }, 1.69 + 1.70 + observe: function(aSubject) 1.71 + { 1.72 + aSubject.QueryInterface(Ci.nsISupportsPRBool); 1.73 + let message = this.expectedValue ? "close" : "stay open"; 1.74 + ok(this.expectedValue === aSubject.data, "Expected browser to " + message); 1.75 + aSubject.data = true; 1.76 + }, 1.77 + 1.78 + uninit: function() 1.79 + { 1.80 + Services.obs.removeObserver(this, "browser-lastwindow-close-requested", false); 1.81 + }, 1.82 +};