michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: 'use strict'; michael@0: michael@0: // Opening new windows in Fennec causes issues michael@0: module.metadata = { michael@0: engines: { michael@0: 'Firefox': '*' michael@0: } michael@0: }; michael@0: michael@0: const { WindowLoader } = require('sdk/windows/loader'), michael@0: { Trait } = require('sdk/deprecated/traits'); michael@0: michael@0: const Loader = Trait.compose( michael@0: WindowLoader, michael@0: { michael@0: constructor: function Loader(options) { michael@0: this._onLoad = options.onLoad; michael@0: this._onUnload = options.onUnload; michael@0: if ('window' in options) michael@0: this._window = options.window; michael@0: this._load(); michael@0: this.window = this._window; michael@0: }, michael@0: window: null, michael@0: _onLoad: null, michael@0: _onUnload: null, michael@0: _tabOptions: [] michael@0: } michael@0: ); michael@0: michael@0: exports['test compositions with missing required properties'] = function(assert) { michael@0: assert.throws( michael@0: function() WindowLoader.compose({})(), michael@0: /Missing required property: _onLoad/, michael@0: 'should throw missing required property exception' michael@0: ); michael@0: assert.throws( michael@0: function() WindowLoader.compose({ _onLoad: null, _tabOptions: null })(), michael@0: /Missing required property: _onUnload/, michael@0: 'should throw missing required property `_onUnload`' michael@0: ); michael@0: assert.throws( michael@0: function() WindowLoader.compose({ _onUnload: null, _tabOptions: null })(), michael@0: /Missing required property: _onLoad/, michael@0: 'should throw missing required property `_onLoad`' michael@0: ); michael@0: assert.throws( michael@0: function() WindowLoader.compose({ _onUnload: null, _onLoad: null })(), michael@0: /Missing required property: _tabOptions/, michael@0: 'should throw missing required property `_tabOptions`' michael@0: ); michael@0: }; michael@0: michael@0: exports['test `load` events'] = function(assert, done) { michael@0: let onLoadCalled = false; michael@0: Loader({ michael@0: onLoad: function(window) { michael@0: onLoadCalled = true; michael@0: assert.equal(window, this._window, 'windows should match'); michael@0: assert.equal( michael@0: window.document.readyState, 'complete', 'window must be fully loaded' michael@0: ); michael@0: window.close(); michael@0: }, michael@0: onUnload: function(window) { michael@0: assert.equal(window, this._window, 'windows should match'); michael@0: assert.equal( michael@0: window.document.readyState, 'complete', 'window must be fully loaded' michael@0: ); michael@0: assert.ok(onLoadCalled, 'load callback is supposed to be called'); michael@0: done(); michael@0: } michael@0: }); michael@0: }; michael@0: michael@0: exports['test removeing listeners'] = function(assert, done) { michael@0: Loader({ michael@0: onLoad: function(window) { michael@0: assert.equal(window, this._window, 'windows should match'); michael@0: window.close(); michael@0: }, michael@0: onUnload: done michael@0: }); michael@0: }; michael@0: michael@0: exports['test create loader from opened window'] = function(assert, done) { michael@0: let onUnloadCalled = false; michael@0: Loader({ michael@0: onLoad: function(window) { michael@0: assert.equal(window, this._window, 'windows should match'); michael@0: assert.equal(window.document.readyState, 'complete', 'window must be fully loaded'); michael@0: Loader({ michael@0: window: window, michael@0: onLoad: function(win) { michael@0: assert.equal(win, window, 'windows should match'); michael@0: window.close(); michael@0: }, michael@0: onUnload: function(window) { michael@0: assert.ok(onUnloadCalled, 'first handler should be called already'); michael@0: done(); michael@0: } michael@0: }); michael@0: }, michael@0: onUnload: function(window) { michael@0: onUnloadCalled = true; michael@0: } michael@0: }); michael@0: }; michael@0: michael@0: require('sdk/test').run(exports);