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 { Ci } = require('chrome'); michael@0: const { open, windows, isBrowser, michael@0: getXULWindow, getBaseWindow, getToplevelWindow, getMostRecentWindow, michael@0: getMostRecentBrowserWindow } = require('sdk/window/utils'); michael@0: const { close } = require('sdk/window/helpers'); michael@0: const windowUtils = require('sdk/deprecated/window-utils'); michael@0: michael@0: exports['test get nsIBaseWindow from nsIDomWindow'] = function(assert) { michael@0: let active = windowUtils.activeBrowserWindow; michael@0: michael@0: assert.ok(!(active instanceof Ci.nsIBaseWindow), michael@0: 'active window is not nsIBaseWindow'); michael@0: michael@0: assert.ok(getBaseWindow(active) instanceof Ci.nsIBaseWindow, michael@0: 'base returns nsIBaseWindow'); michael@0: }; michael@0: michael@0: exports['test get nsIXULWindow from nsIDomWindow'] = function(assert) { michael@0: let active = windowUtils.activeBrowserWindow; michael@0: assert.ok(!(active instanceof Ci.nsIXULWindow), michael@0: 'active window is not nsIXULWindow'); michael@0: assert.ok(getXULWindow(active) instanceof Ci.nsIXULWindow, michael@0: 'base returns nsIXULWindow'); michael@0: }; michael@0: michael@0: exports['test getToplevelWindow'] = function(assert) { michael@0: let active = windowUtils.activeBrowserWindow; michael@0: assert.equal(getToplevelWindow(active), active, michael@0: 'getToplevelWindow of toplevel window returns the same window'); michael@0: assert.equal(getToplevelWindow(active.content), active, michael@0: 'getToplevelWindow of tab window returns the browser window'); michael@0: assert.ok(getToplevelWindow(active) instanceof Ci.nsIDOMWindow, michael@0: 'getToplevelWindow returns nsIDOMWindow'); michael@0: }; michael@0: michael@0: exports['test top window creation'] = function(assert, done) { michael@0: let window = open('data:text/html;charset=utf-8,Hello top window'); michael@0: assert.ok(~windows().indexOf(window), 'window was opened'); michael@0: michael@0: // Wait for the window unload before ending test michael@0: close(window).then(done); michael@0: }; michael@0: michael@0: exports['test new top window with options'] = function(assert, done) { michael@0: let window = open('data:text/html;charset=utf-8,Hi custom top window', { michael@0: name: 'test', michael@0: features: { height: 100, width: 200, toolbar: true } michael@0: }); michael@0: assert.ok(~windows().indexOf(window), 'window was opened'); michael@0: assert.equal(window.name, 'test', 'name was set'); michael@0: assert.equal(window.innerHeight, 100, 'height is set'); michael@0: assert.equal(window.innerWidth, 200, 'height is set'); michael@0: assert.equal(window.toolbar.visible, true, 'toolbar was set'); michael@0: michael@0: // Wait for the window unload before ending test michael@0: close(window).then(done); michael@0: }; michael@0: michael@0: exports['test new top window with various URIs'] = function(assert, done) { michael@0: let msg = 'only chrome, resource and data uris are allowed'; michael@0: assert.throws(function () { michael@0: open('foo'); michael@0: }, msg); michael@0: assert.throws(function () { michael@0: open('http://foo'); michael@0: }, msg); michael@0: assert.throws(function () { michael@0: open('https://foo'); michael@0: }, msg); michael@0: assert.throws(function () { michael@0: open('ftp://foo'); michael@0: }, msg); michael@0: assert.throws(function () { michael@0: open('//foo'); michael@0: }, msg); michael@0: michael@0: let chromeWindow = open('chrome://foo/content/'); michael@0: assert.ok(~windows().indexOf(chromeWindow), 'chrome URI works'); michael@0: michael@0: let resourceWindow = open('resource://foo'); michael@0: assert.ok(~windows().indexOf(resourceWindow), 'resource URI works'); michael@0: michael@0: // Wait for the window unload before ending test michael@0: close(chromeWindow).then(close.bind(null, resourceWindow)).then(done); michael@0: }; michael@0: michael@0: exports.testIsBrowser = function(assert) { michael@0: // dummy window, bad type michael@0: assert.equal(isBrowser({ document: { documentElement: { getAttribute: function() { michael@0: return 'navigator:browserx'; michael@0: }}}}), false, 'dummy object with correct stucture and bad type does not pass'); michael@0: michael@0: assert.ok(isBrowser(getMostRecentBrowserWindow()), 'active browser window is a browser window'); michael@0: assert.ok(!isBrowser({}), 'non window is not a browser window'); michael@0: assert.ok(!isBrowser({ document: {} }), 'non window is not a browser window'); michael@0: assert.ok(!isBrowser({ document: { documentElement: {} } }), 'non window is not a browser window'); michael@0: assert.ok(!isBrowser(), 'no argument is not a browser window'); michael@0: }; michael@0: michael@0: require('test').run(exports);