summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/test/event/helpers.js
blob: d0d63d46f8a373b067aaaa160b73d8185e8afc8d (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
/* 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 { on, once, off, emit, count } = require("sdk/event/core");

const { setImmediate, setTimeout } = require("sdk/timers");
const { defer } = require("sdk/core/promise");

/**
 * Utility function that returns a promise once the specified event's `type`
 * is emitted on the given `target`, or the delay specified is passed.
 *
 * @param {Object|Number} [target]
 *    The delay to wait, or the object that receives the event.
 *    If not given, the function returns a promise that will be resolved
 *    as soon as possible.
 * @param {String} [type]
 *    A string representing the event type to waiting for.
 * @param {Boolean} [capture]
 *    If `true`, `capture` indicates that the user wishes to initiate capture.
 *
 * @returns {Promise}
 *    A promise resolved once the delay given is passed, or the object
 *    receives the event specified
 */
const wait = function(target, type, capture) {
  let { promise, resolve, reject } = defer();

  if (!arguments.length) {
    setImmediate(resolve);
  }
  else if (typeof(target) === "number") {
    setTimeout(resolve, target);
  }
  else if (typeof(target.once) === "function") {
    target.once(type, resolve);
  }
  else if (typeof(target.addEventListener) === "function") {
    target.addEventListener(type, function listener(...args) {
      this.removeEventListener(type, listener, capture);
      resolve(...args);
    }, capture);
  }
  else if (typeof(target) === "object" && target !== null) {
    once(target, type, resolve);
  }
  else {
    reject('Invalid target given.');
  }

  return promise;
};
exports.wait = wait;

function scenario(setup) {
  return function(unit) {
    return function(assert) {
      let actual = [];
      let input = {};
      unit(input, function(output, events, expected, message) {
        let result = setup(output, expected, actual);

        events.forEach(event => emit(input, "data", event));

        assert.deepEqual(actual, result, message);
      });
    }
  }
}

exports.emits = scenario(function(output, expected, actual) {
  on(output, "data", function(data) {
    return actual.push(this, data);
  });

  return expected.reduce(($$, $) => $$.concat(output, $), []);
});

exports.registerOnce = scenario(function(output, expected, actual) {
  function listener(data) {
    return actual.push(data);
  }
  on(output, "data", listener);
  on(output, "data", listener);
  on(output, "data", listener);

  return expected;
});

exports.ignoreNew = scenario(function(output, expected, actual) {
  on(output, "data", function(data) {
    actual.push(data + "#1");
    on(output, "data", function(data) {
      actual.push(data + "#2");
    });
  });

  return expected.map($ => $ + "#1");
});

exports.FIFO = scenario(function(target, expected, actual) {
  on(target, "data", $ => actual.push($ + "#1"));
  on(target, "data", $ => actual.push($ + "#2"));
  on(target, "data", $ => actual.push($ + "#3"));

  return expected.reduce(function(result, value) {
    return result.concat(value + "#1", value + "#2", value + "#3");
  }, []);
});