|
1 /* vim: set ts=2 et sw=2 tw=80: */ |
|
2 /* Any copyright is dedicated to the Public Domain. |
|
3 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
4 |
|
5 const BUTTON_POSITION_CANCEL = 1; |
|
6 const BUTTON_POSITION_DONT_SAVE = 2; |
|
7 |
|
8 |
|
9 function test() |
|
10 { |
|
11 waitForExplicitFinish(); |
|
12 |
|
13 // Observer must be attached *before* Scratchpad is opened. |
|
14 CloseObserver.init(); |
|
15 |
|
16 gBrowser.selectedTab = gBrowser.addTab(); |
|
17 gBrowser.selectedBrowser.addEventListener("load", function onTabLoad() { |
|
18 gBrowser.selectedBrowser.removeEventListener("load", onTabLoad, true); |
|
19 openScratchpad(runTests); |
|
20 }, true); |
|
21 |
|
22 content.location = "data:text/html;charset=utf8,<p>test browser last window closing</p>"; |
|
23 } |
|
24 |
|
25 |
|
26 |
|
27 function runTests({ Scratchpad }) |
|
28 { |
|
29 let browser = Services.wm.getEnumerator("navigator:browser").getNext(); |
|
30 let oldPrompt = Services.prompt; |
|
31 let button; |
|
32 |
|
33 Services.prompt = { |
|
34 confirmEx: () => button |
|
35 }; |
|
36 |
|
37 |
|
38 Scratchpad.dirty = true; |
|
39 |
|
40 // Test canceling close. |
|
41 button = BUTTON_POSITION_CANCEL; |
|
42 CloseObserver.expectedValue = true; |
|
43 browser.BrowserTryToCloseWindow(); |
|
44 |
|
45 // Test accepting close. |
|
46 button = BUTTON_POSITION_DONT_SAVE; |
|
47 CloseObserver.expectedValue = false; |
|
48 browser.BrowserTryToCloseWindow(); |
|
49 |
|
50 // Test closing without prompt needed. |
|
51 Scratchpad.dirty = false; |
|
52 browser.BrowserTryToCloseWindow(); |
|
53 |
|
54 Services.prompt = oldPrompt; |
|
55 CloseObserver.uninit(); |
|
56 finish(); |
|
57 } |
|
58 |
|
59 |
|
60 let CloseObserver = { |
|
61 expectedValue: null, |
|
62 init: function() |
|
63 { |
|
64 Services.obs.addObserver(this, "browser-lastwindow-close-requested", false); |
|
65 }, |
|
66 |
|
67 observe: function(aSubject) |
|
68 { |
|
69 aSubject.QueryInterface(Ci.nsISupportsPRBool); |
|
70 let message = this.expectedValue ? "close" : "stay open"; |
|
71 ok(this.expectedValue === aSubject.data, "Expected browser to " + message); |
|
72 aSubject.data = true; |
|
73 }, |
|
74 |
|
75 uninit: function() |
|
76 { |
|
77 Services.obs.removeObserver(this, "browser-lastwindow-close-requested", false); |
|
78 }, |
|
79 }; |