1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/test-window-loader.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,113 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 +'use strict'; 1.8 + 1.9 +// Opening new windows in Fennec causes issues 1.10 +module.metadata = { 1.11 + engines: { 1.12 + 'Firefox': '*' 1.13 + } 1.14 +}; 1.15 + 1.16 +const { WindowLoader } = require('sdk/windows/loader'), 1.17 + { Trait } = require('sdk/deprecated/traits'); 1.18 + 1.19 +const Loader = Trait.compose( 1.20 + WindowLoader, 1.21 + { 1.22 + constructor: function Loader(options) { 1.23 + this._onLoad = options.onLoad; 1.24 + this._onUnload = options.onUnload; 1.25 + if ('window' in options) 1.26 + this._window = options.window; 1.27 + this._load(); 1.28 + this.window = this._window; 1.29 + }, 1.30 + window: null, 1.31 + _onLoad: null, 1.32 + _onUnload: null, 1.33 + _tabOptions: [] 1.34 + } 1.35 +); 1.36 + 1.37 +exports['test compositions with missing required properties'] = function(assert) { 1.38 + assert.throws( 1.39 + function() WindowLoader.compose({})(), 1.40 + /Missing required property: _onLoad/, 1.41 + 'should throw missing required property exception' 1.42 + ); 1.43 + assert.throws( 1.44 + function() WindowLoader.compose({ _onLoad: null, _tabOptions: null })(), 1.45 + /Missing required property: _onUnload/, 1.46 + 'should throw missing required property `_onUnload`' 1.47 + ); 1.48 + assert.throws( 1.49 + function() WindowLoader.compose({ _onUnload: null, _tabOptions: null })(), 1.50 + /Missing required property: _onLoad/, 1.51 + 'should throw missing required property `_onLoad`' 1.52 + ); 1.53 + assert.throws( 1.54 + function() WindowLoader.compose({ _onUnload: null, _onLoad: null })(), 1.55 + /Missing required property: _tabOptions/, 1.56 + 'should throw missing required property `_tabOptions`' 1.57 + ); 1.58 +}; 1.59 + 1.60 +exports['test `load` events'] = function(assert, done) { 1.61 + let onLoadCalled = false; 1.62 + Loader({ 1.63 + onLoad: function(window) { 1.64 + onLoadCalled = true; 1.65 + assert.equal(window, this._window, 'windows should match'); 1.66 + assert.equal( 1.67 + window.document.readyState, 'complete', 'window must be fully loaded' 1.68 + ); 1.69 + window.close(); 1.70 + }, 1.71 + onUnload: function(window) { 1.72 + assert.equal(window, this._window, 'windows should match'); 1.73 + assert.equal( 1.74 + window.document.readyState, 'complete', 'window must be fully loaded' 1.75 + ); 1.76 + assert.ok(onLoadCalled, 'load callback is supposed to be called'); 1.77 + done(); 1.78 + } 1.79 + }); 1.80 +}; 1.81 + 1.82 +exports['test removeing listeners'] = function(assert, done) { 1.83 + Loader({ 1.84 + onLoad: function(window) { 1.85 + assert.equal(window, this._window, 'windows should match'); 1.86 + window.close(); 1.87 + }, 1.88 + onUnload: done 1.89 + }); 1.90 +}; 1.91 + 1.92 +exports['test create loader from opened window'] = function(assert, done) { 1.93 + let onUnloadCalled = false; 1.94 + Loader({ 1.95 + onLoad: function(window) { 1.96 + assert.equal(window, this._window, 'windows should match'); 1.97 + assert.equal(window.document.readyState, 'complete', 'window must be fully loaded'); 1.98 + Loader({ 1.99 + window: window, 1.100 + onLoad: function(win) { 1.101 + assert.equal(win, window, 'windows should match'); 1.102 + window.close(); 1.103 + }, 1.104 + onUnload: function(window) { 1.105 + assert.ok(onUnloadCalled, 'first handler should be called already'); 1.106 + done(); 1.107 + } 1.108 + }); 1.109 + }, 1.110 + onUnload: function(window) { 1.111 + onUnloadCalled = true; 1.112 + } 1.113 + }); 1.114 +}; 1.115 + 1.116 +require('sdk/test').run(exports);