|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 "use strict"; |
|
5 |
|
6 const { Loader } = require("sdk/test/loader"); |
|
7 const { open, close } = require("sdk/window/helpers"); |
|
8 const { browserWindows: windows } = require("sdk/windows"); |
|
9 const { isBrowser } = require('sdk/window/utils'); |
|
10 const app = require("sdk/system/xul-app"); |
|
11 |
|
12 exports["test unload window observer"] = function(assert, done) { |
|
13 // Hacky way to be able to create unloadable modules via makeSandboxedLoader. |
|
14 let loader = Loader(module); |
|
15 let observer = loader.require("sdk/windows/observer").observer; |
|
16 let opened = 0; |
|
17 let closed = 0; |
|
18 let windowsOpen = windows.length; |
|
19 |
|
20 observer.on("open", onOpen); |
|
21 observer.on("close", onClose); |
|
22 |
|
23 // On Fennec, only test that the module does not throw an error |
|
24 if (app.is("Fennec")) { |
|
25 assert.pass("Windows observer did not throw on Fennec"); |
|
26 return cleanUp(); |
|
27 } |
|
28 |
|
29 // Open window and close it to trigger observers. |
|
30 open(). |
|
31 then(close). |
|
32 then(loader.unload). |
|
33 then(open). |
|
34 then(close). |
|
35 then(function() { |
|
36 // Enqueuing asserts to make sure that assertion is not performed early. |
|
37 assert.equal(1, opened, "observer open was called before unload only"); |
|
38 assert.equal(windowsOpen + 1, closed, "observer close was called before unload only"); |
|
39 }). |
|
40 then(cleanUp, assert.fail); |
|
41 |
|
42 function cleanUp () { |
|
43 observer.removeListener("open", onOpen); |
|
44 observer.removeListener("close", onClose); |
|
45 done(); |
|
46 } |
|
47 |
|
48 function onOpen(window) { |
|
49 // Ignoring non-browser windows |
|
50 if (isBrowser(window)) |
|
51 opened++; |
|
52 } |
|
53 function onClose(window) { |
|
54 // Ignore non-browser windows & already opened `activeWindow` (unload will |
|
55 // emit close on it even though it is not actually closed). |
|
56 if (isBrowser(window)) |
|
57 closed++; |
|
58 } |
|
59 }; |
|
60 |
|
61 require("sdk/test").run(exports); |