test-window-utils2.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // Opening new windows in Fennec causes issues
  6. module.metadata = {
  7. engines: {
  8. 'Firefox': '*'
  9. }
  10. };
  11. const { Ci } = require('chrome');
  12. const { open, windows, isBrowser,
  13. getXULWindow, getBaseWindow, getToplevelWindow, getMostRecentWindow,
  14. getMostRecentBrowserWindow } = require('sdk/window/utils');
  15. const { close } = require('sdk/window/helpers');
  16. const windowUtils = require('sdk/deprecated/window-utils');
  17. exports['test get nsIBaseWindow from nsIDomWindow'] = function(assert) {
  18. let active = windowUtils.activeBrowserWindow;
  19. assert.ok(!(active instanceof Ci.nsIBaseWindow),
  20. 'active window is not nsIBaseWindow');
  21. assert.ok(getBaseWindow(active) instanceof Ci.nsIBaseWindow,
  22. 'base returns nsIBaseWindow');
  23. };
  24. exports['test get nsIXULWindow from nsIDomWindow'] = function(assert) {
  25. let active = windowUtils.activeBrowserWindow;
  26. assert.ok(!(active instanceof Ci.nsIXULWindow),
  27. 'active window is not nsIXULWindow');
  28. assert.ok(getXULWindow(active) instanceof Ci.nsIXULWindow,
  29. 'base returns nsIXULWindow');
  30. };
  31. exports['test getToplevelWindow'] = function(assert) {
  32. let active = windowUtils.activeBrowserWindow;
  33. assert.equal(getToplevelWindow(active), active,
  34. 'getToplevelWindow of toplevel window returns the same window');
  35. assert.equal(getToplevelWindow(active.content), active,
  36. 'getToplevelWindow of tab window returns the browser window');
  37. assert.ok(getToplevelWindow(active) instanceof Ci.nsIDOMWindow,
  38. 'getToplevelWindow returns nsIDOMWindow');
  39. };
  40. exports['test top window creation'] = function(assert, done) {
  41. let window = open('data:text/html;charset=utf-8,Hello top window');
  42. assert.ok(~windows().indexOf(window), 'window was opened');
  43. // Wait for the window unload before ending test
  44. close(window).then(done);
  45. };
  46. exports['test new top window with options'] = function(assert, done) {
  47. let window = open('data:text/html;charset=utf-8,Hi custom top window', {
  48. name: 'test',
  49. features: { height: 100, width: 200, toolbar: true }
  50. });
  51. assert.ok(~windows().indexOf(window), 'window was opened');
  52. assert.equal(window.name, 'test', 'name was set');
  53. assert.equal(window.innerHeight, 100, 'height is set');
  54. assert.equal(window.innerWidth, 200, 'height is set');
  55. assert.equal(window.toolbar.visible, true, 'toolbar was set');
  56. // Wait for the window unload before ending test
  57. close(window).then(done);
  58. };
  59. exports['test new top window with various URIs'] = function(assert, done) {
  60. let msg = 'only chrome, resource and data uris are allowed';
  61. assert.throws(function () {
  62. open('foo');
  63. }, msg);
  64. assert.throws(function () {
  65. open('http://foo');
  66. }, msg);
  67. assert.throws(function () {
  68. open('https://foo');
  69. }, msg);
  70. assert.throws(function () {
  71. open('ftp://foo');
  72. }, msg);
  73. assert.throws(function () {
  74. open('//foo');
  75. }, msg);
  76. let chromeWindow = open('chrome://foo/content/');
  77. assert.ok(~windows().indexOf(chromeWindow), 'chrome URI works');
  78. let resourceWindow = open('resource://foo');
  79. assert.ok(~windows().indexOf(resourceWindow), 'resource URI works');
  80. // Wait for the window unload before ending test
  81. close(chromeWindow).then(close.bind(null, resourceWindow)).then(done);
  82. };
  83. exports.testIsBrowser = function(assert) {
  84. // dummy window, bad type
  85. assert.equal(isBrowser({ document: { documentElement: { getAttribute: function() {
  86. return 'navigator:browserx';
  87. }}}}), false, 'dummy object with correct stucture and bad type does not pass');
  88. assert.ok(isBrowser(getMostRecentBrowserWindow()), 'active browser window is a browser window');
  89. assert.ok(!isBrowser({}), 'non window is not a browser window');
  90. assert.ok(!isBrowser({ document: {} }), 'non window is not a browser window');
  91. assert.ok(!isBrowser({ document: { documentElement: {} } }), 'non window is not a browser window');
  92. assert.ok(!isBrowser(), 'no argument is not a browser window');
  93. };
  94. require('test').run(exports);