addon-sdk/source/test/test-window-utils2.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-window-utils2.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,112 @@
     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 { Ci } = require('chrome');
    1.17 +const { open, windows, isBrowser,
    1.18 +        getXULWindow, getBaseWindow, getToplevelWindow, getMostRecentWindow,
    1.19 +        getMostRecentBrowserWindow } = require('sdk/window/utils');
    1.20 +const { close } = require('sdk/window/helpers');
    1.21 +const windowUtils = require('sdk/deprecated/window-utils');
    1.22 +
    1.23 +exports['test get nsIBaseWindow from nsIDomWindow'] = function(assert) {
    1.24 +  let active = windowUtils.activeBrowserWindow;
    1.25 +
    1.26 +  assert.ok(!(active instanceof Ci.nsIBaseWindow),
    1.27 +            'active window is not nsIBaseWindow');
    1.28 +
    1.29 +  assert.ok(getBaseWindow(active) instanceof Ci.nsIBaseWindow,
    1.30 +            'base returns nsIBaseWindow');
    1.31 +};
    1.32 +
    1.33 +exports['test get nsIXULWindow from nsIDomWindow'] = function(assert) {
    1.34 +  let active = windowUtils.activeBrowserWindow;
    1.35 +  assert.ok(!(active instanceof Ci.nsIXULWindow),
    1.36 +            'active window is not nsIXULWindow');
    1.37 +  assert.ok(getXULWindow(active) instanceof Ci.nsIXULWindow,
    1.38 +            'base returns nsIXULWindow');
    1.39 +};
    1.40 +
    1.41 +exports['test getToplevelWindow'] = function(assert) {
    1.42 +  let active = windowUtils.activeBrowserWindow;
    1.43 +  assert.equal(getToplevelWindow(active), active,
    1.44 +               'getToplevelWindow of toplevel window returns the same window');
    1.45 +  assert.equal(getToplevelWindow(active.content), active,
    1.46 +               'getToplevelWindow of tab window returns the browser window');
    1.47 +  assert.ok(getToplevelWindow(active) instanceof Ci.nsIDOMWindow,
    1.48 +            'getToplevelWindow returns nsIDOMWindow');
    1.49 +};
    1.50 +
    1.51 +exports['test top window creation'] = function(assert, done) {
    1.52 +  let window = open('data:text/html;charset=utf-8,Hello top window');
    1.53 +  assert.ok(~windows().indexOf(window), 'window was opened');
    1.54 +
    1.55 +  // Wait for the window unload before ending test
    1.56 +  close(window).then(done);
    1.57 +};
    1.58 +
    1.59 +exports['test new top window with options'] = function(assert, done) {
    1.60 +  let window = open('data:text/html;charset=utf-8,Hi custom top window', {
    1.61 +    name: 'test',
    1.62 +    features: { height: 100, width: 200, toolbar: true }
    1.63 +  });
    1.64 +  assert.ok(~windows().indexOf(window), 'window was opened');
    1.65 +  assert.equal(window.name, 'test', 'name was set');
    1.66 +  assert.equal(window.innerHeight, 100, 'height is set');
    1.67 +  assert.equal(window.innerWidth, 200, 'height is set');
    1.68 +  assert.equal(window.toolbar.visible, true, 'toolbar was set');
    1.69 +
    1.70 +  // Wait for the window unload before ending test
    1.71 +  close(window).then(done);
    1.72 +};
    1.73 +
    1.74 +exports['test new top window with various URIs'] = function(assert, done) {
    1.75 +  let msg = 'only chrome, resource and data uris are allowed';
    1.76 +  assert.throws(function () {
    1.77 +    open('foo');
    1.78 +  }, msg);
    1.79 +  assert.throws(function () {
    1.80 +    open('http://foo');
    1.81 +  }, msg);
    1.82 +  assert.throws(function () {
    1.83 +    open('https://foo');
    1.84 +  }, msg);
    1.85 +  assert.throws(function () {
    1.86 +    open('ftp://foo');
    1.87 +  }, msg);
    1.88 +  assert.throws(function () {
    1.89 +    open('//foo');
    1.90 +  }, msg);
    1.91 +
    1.92 +  let chromeWindow = open('chrome://foo/content/');
    1.93 +  assert.ok(~windows().indexOf(chromeWindow), 'chrome URI works');
    1.94 +
    1.95 +  let resourceWindow = open('resource://foo');
    1.96 +  assert.ok(~windows().indexOf(resourceWindow), 'resource URI works');
    1.97 +
    1.98 +  // Wait for the window unload before ending test
    1.99 +  close(chromeWindow).then(close.bind(null, resourceWindow)).then(done);
   1.100 +};
   1.101 +
   1.102 +exports.testIsBrowser = function(assert) {
   1.103 +  // dummy window, bad type
   1.104 +  assert.equal(isBrowser({ document: { documentElement: { getAttribute: function() {
   1.105 +    return 'navigator:browserx';
   1.106 +  }}}}), false, 'dummy object with correct stucture and bad type does not pass');
   1.107 +
   1.108 +  assert.ok(isBrowser(getMostRecentBrowserWindow()), 'active browser window is a browser window');
   1.109 +  assert.ok(!isBrowser({}), 'non window is not a browser window');
   1.110 +  assert.ok(!isBrowser({ document: {} }), 'non window is not a browser window');
   1.111 +  assert.ok(!isBrowser({ document: { documentElement: {} } }), 'non window is not a browser window');
   1.112 +  assert.ok(!isBrowser(), 'no argument is not a browser window');
   1.113 +};
   1.114 +
   1.115 +require('test').run(exports);

mercurial