aboutsummaryrefslogtreecommitdiff
path: root/tools/addon-sdk-1.5/packages/api-utils/tests/test-window-loader.js
blob: 0a5ab8427842ff70e88791d1c6aebd77358ada3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

const { WindowLoader } = require('windows/loader'),
      { Trait } = require('traits');

const Loader = Trait.compose(
  WindowLoader,
  {
    constructor: function Loader(options) {
      this._onLoad = options.onLoad;
      this._onUnload = options.onUnload;
      if ('window' in options)
        this._window = options.window;
      this._load();
      this.window = this._window;
    },
    window: null,
    _onLoad: null,
    _onUnload: null,
    _tabOptions: []
  }
);

exports['test compositions with missing required properties'] = function(test) {
  test.assertRaises(
    function() WindowLoader.compose({})(),
    'Missing required property: _onLoad',
    'should throw missing required property exception'
  );
  test.assertRaises(
    function() WindowLoader.compose({ _onLoad: null, _tabOptions: null })(),
    'Missing required property: _onUnload',
    'should throw missing required property `_onUnload`'
  );
  test.assertRaises(
    function() WindowLoader.compose({ _onUnload: null, _tabOptions: null })(),
    'Missing required property: _onLoad',
    'should throw missing required property `_onLoad`'
  );
  test.assertRaises(
    function() WindowLoader.compose({ _onUnload: null, _onLoad: null })(),
    'Missing required property: _tabOptions',
    'should throw missing required property `_tabOptions`'
  );
};

exports['test `load` events'] = function(test) {
  test.waitUntilDone();
  let onLoadCalled = false;
  Loader({
    onLoad: function(window) {
      onLoadCalled = true;
      test.assertEqual(
        window, this._window, 'windows should match'
      );
      test.assertEqual(
        window.document.readyState, 'complete', 'window must be fully loaded'
      );
      window.close();
    },
    onUnload: function(window) {
      test.assertEqual(
        window, this._window, 'windows should match'
      );
      test.assertEqual(
        window.document.readyState, 'complete', 'window must be fully loaded'
      );
      test.assert(onLoadCalled, 'load callback is supposed to be called');
      test.done();
    }
  });
};

exports['test removeing listeners'] = function(test) {
  test.waitUntilDone();
  Loader({
    onLoad: function(window) {
      test.assertEqual(
        window, this._window, 'windows should match'
      );
      window.close();
    },
    onUnload: function(window) {
      test.done();
    }
  });
};

exports['test create loader from opened window'] = function(test) {
  test.waitUntilDone();
  let onUnloadCalled = false;
  Loader({
    onLoad: function(window) {
      test.assertEqual(
        window, this._window, 'windows should match'
      );
      test.assertEqual(
        window.document.readyState, 'complete', 'window must be fully loaded'
      );
      Loader({
        window: window,
        onLoad: function(win) {
          test.assertEqual(win, window, 'windows should match');
          window.close();
        },
        onUnload: function(window) {
          test.assert(onUnloadCalled, 'first handler should be called already');
          test.done();
        }
      });
    },
    onUnload: function(window) {
      onUnloadCalled = true;
    }
  });
};