test-window-loader.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 { WindowLoader } = require('sdk/windows/loader'),
  12. { Trait } = require('sdk/deprecated/traits');
  13. const Loader = Trait.compose(
  14. WindowLoader,
  15. {
  16. constructor: function Loader(options) {
  17. this._onLoad = options.onLoad;
  18. this._onUnload = options.onUnload;
  19. if ('window' in options)
  20. this._window = options.window;
  21. this._load();
  22. this.window = this._window;
  23. },
  24. window: null,
  25. _onLoad: null,
  26. _onUnload: null,
  27. _tabOptions: []
  28. }
  29. );
  30. exports['test compositions with missing required properties'] = function(assert) {
  31. assert.throws(
  32. function() WindowLoader.compose({})(),
  33. /Missing required property: _onLoad/,
  34. 'should throw missing required property exception'
  35. );
  36. assert.throws(
  37. function() WindowLoader.compose({ _onLoad: null, _tabOptions: null })(),
  38. /Missing required property: _onUnload/,
  39. 'should throw missing required property `_onUnload`'
  40. );
  41. assert.throws(
  42. function() WindowLoader.compose({ _onUnload: null, _tabOptions: null })(),
  43. /Missing required property: _onLoad/,
  44. 'should throw missing required property `_onLoad`'
  45. );
  46. assert.throws(
  47. function() WindowLoader.compose({ _onUnload: null, _onLoad: null })(),
  48. /Missing required property: _tabOptions/,
  49. 'should throw missing required property `_tabOptions`'
  50. );
  51. };
  52. exports['test `load` events'] = function(assert, done) {
  53. let onLoadCalled = false;
  54. Loader({
  55. onLoad: function(window) {
  56. onLoadCalled = true;
  57. assert.equal(window, this._window, 'windows should match');
  58. assert.equal(
  59. window.document.readyState, 'complete', 'window must be fully loaded'
  60. );
  61. window.close();
  62. },
  63. onUnload: function(window) {
  64. assert.equal(window, this._window, 'windows should match');
  65. assert.equal(
  66. window.document.readyState, 'complete', 'window must be fully loaded'
  67. );
  68. assert.ok(onLoadCalled, 'load callback is supposed to be called');
  69. done();
  70. }
  71. });
  72. };
  73. exports['test removeing listeners'] = function(assert, done) {
  74. Loader({
  75. onLoad: function(window) {
  76. assert.equal(window, this._window, 'windows should match');
  77. window.close();
  78. },
  79. onUnload: done
  80. });
  81. };
  82. exports['test create loader from opened window'] = function(assert, done) {
  83. let onUnloadCalled = false;
  84. Loader({
  85. onLoad: function(window) {
  86. assert.equal(window, this._window, 'windows should match');
  87. assert.equal(window.document.readyState, 'complete', 'window must be fully loaded');
  88. Loader({
  89. window: window,
  90. onLoad: function(win) {
  91. assert.equal(win, window, 'windows should match');
  92. window.close();
  93. },
  94. onUnload: function(window) {
  95. assert.ok(onUnloadCalled, 'first handler should be called already');
  96. done();
  97. }
  98. });
  99. },
  100. onUnload: function(window) {
  101. onUnloadCalled = true;
  102. }
  103. });
  104. };
  105. require('sdk/test').run(exports);