summaryrefslogtreecommitdiff
path: root/addon-sdk/source/test/test-event-target.js
blob: d51314aa5108d9c1028af4edc805ba7af1b8f4fe (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* 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 { emit } = require('sdk/event/core');
const { EventTarget } = require('sdk/event/target');
const { Loader } = require('sdk/test/loader');

exports['test add a listener'] = function(assert) {
  let events = [ { name: 'event#1' }, 'event#2' ];
  let target = EventTarget();

  target.on('message', function(message) {
    assert.equal(this, target, 'this is a target object');
    assert.equal(message, events.shift(), 'message is emitted event');
  });

  emit(target, 'message', events[0]);
  emit(target, 'message', events[0]);
};

exports['test pass in listeners'] = function(assert) {
  let actual = [ ];
  let target = EventTarget({
    onMessage: function onMessage(message) {
      assert.equal(this, target, 'this is an event target');
      actual.push(1);
    },
    onFoo: null,
    onbla: function() {
      assert.fail('`onbla` is not supposed to be called');
    }
  });
  target.on('message', function(message) {
    assert.equal(this, target, 'this is an event target');
    actual.push(2);
  });

  emit(target, 'message');
  emit(target, 'missing');

  assert.deepEqual([ 1, 2 ], actual, 'all listeners trigerred in right order');
};

exports['test that listener is unique per type'] = function(assert) {
  let actual = []
  let target = EventTarget();
  function listener() { actual.push(1) }
  target.on('message', listener);
  target.on('message', listener);
  target.on('message', listener);
  target.on('foo', listener);
  target.on('foo', listener);

  emit(target, 'message');
  assert.deepEqual([ 1 ], actual, 'only one message listener added');
  emit(target, 'foo');
  assert.deepEqual([ 1, 1 ], actual, 'same listener added for other event');
};

exports['test event type matters'] = function(assert) {
  let target = EventTarget();
  target.on('message', function() {
    assert.fail('no event is expected');
  });
  target.on('done', function() {
    assert.pass('event is emitted');
  });

  emit(target, 'foo');
  emit(target, 'done');
};

exports['test all arguments are pasesd'] = function(assert) {
  let foo = { name: 'foo' }, bar = 'bar';
  let target = EventTarget();
  target.on('message', function(a, b) {
    assert.equal(a, foo, 'first argument passed');
    assert.equal(b, bar, 'second argument passed');
  });
  emit(target, 'message', foo, bar);
};

exports['test no side-effects in emit'] = function(assert) {
  let target = EventTarget();
  target.on('message', function() {
    assert.pass('first listener is called');
    target.on('message', function() {
      assert.fail('second listener is called');
    });
  });
  emit(target, 'message');
};

exports['test order of propagation'] = function(assert) {
  let actual = [];
  let target = EventTarget();
  target.on('message', function() { actual.push(1); });
  target.on('message', function() { actual.push(2); });
  target.on('message', function() { actual.push(3); });
  emit(target, 'message');
  assert.deepEqual([ 1, 2, 3 ], actual, 'called in order they were added');
};

exports['test remove a listener'] = function(assert) {
  let target = EventTarget();
  let actual = [];
  target.on('message', function listener() {
    actual.push(1);
    target.on('message', function() {
      target.removeListener('message', listener);
      actual.push(2);
    })
  });

  emit(target, 'message');
  assert.deepEqual([ 1 ], actual, 'first listener called');
  emit(target, 'message');
  assert.deepEqual([ 1, 1, 2 ], actual, 'second listener called');
  emit(target, 'message');
  assert.deepEqual([ 1, 1, 2, 2, 2 ], actual, 'first listener removed');
};

exports['test .off() removes all listeners'] = function(assert) {
  let target = EventTarget();
  let actual = [];
  target.on('message', function listener() {
    actual.push(1);
    target.on('message', function() {
      target.removeListener('message', listener);
      actual.push(2);
    })
  });

  emit(target, 'message');
  assert.deepEqual([ 1 ], actual, 'first listener called');
  emit(target, 'message');
  assert.deepEqual([ 1, 1, 2 ], actual, 'second listener called');
  target.off();
  emit(target, 'message');
  assert.deepEqual([ 1, 1, 2 ], actual, 'target.off() removed all listeners');
};

exports['test error handling'] = function(assert) {
  let target = EventTarget();
  let error = Error('boom!');

  target.on('message', function() { throw error; })
  target.on('error', function(boom) {
    assert.equal(boom, error, 'thrown exception causes error event');
  });
  emit(target, 'message');
};

exports['test unhandled errors'] = function(assert) {
  let exceptions = [];
  let loader = Loader(module);
  let { emit } = loader.require('sdk/event/core');
  let { EventTarget } = loader.require('sdk/event/target');
  Object.defineProperties(loader.sandbox('sdk/event/core'), {
    console: { value: {
      exception: function(e) {
        exceptions.push(e);
      }
    }}
  });
  let target = EventTarget();
  let boom = Error('Boom!');
  let drax = Error('Draax!!');

  target.on('message', function() { throw boom; });

  emit(target, 'message');
  assert.ok(~String(exceptions[0]).indexOf('Boom!'),
            'unhandled exception is logged');

  target.on('error', function() { throw drax; });
  emit(target, 'message');
  assert.ok(~String(exceptions[1]).indexOf('Draax!'),
            'error in error handler is logged');
};

exports['test target is chainable'] = function (assert, done) {
  let loader = Loader(module);
  let exceptions = [];
  let { EventTarget } = loader.require('sdk/event/target');
  let { emit } = loader.require('sdk/event/core');
  Object.defineProperties(loader.sandbox('sdk/event/core'), {
    console: { value: {
      exception: function(e) {
        exceptions.push(e);
      }
    }}
  });

  let emitter = EventTarget();
  let boom = Error('Boom');
  let onceCalled = 0;
  
  emitter.once('oneTime', function () {
    assert.equal(++onceCalled, 1, 'once event called only once');
  }).on('data', function (message) {
    assert.equal(message, 'message', 'handles event');
    emit(emitter, 'oneTime');
    emit(emitter, 'data2', 'message2');
  }).on('phony', function () {
    assert.fail('removeListener does not remove chained event');
  }).removeListener('phony')
  .on('data2', function (message) {
    assert.equal(message, 'message2', 'handle chained event');
    emit(emitter, 'oneTime');
    throw boom;
  }).on('error', function (error) {
    assert.equal(error, boom, 'error handled in chained event');
    done();
  });

  emit(emitter, 'data', 'message');
};

require('sdk/test').run(exports);