addon-sdk/source/test/test-event-utils.js

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     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/. */
     5 'use strict';
     7 const { on, emit } = require("sdk/event/core");
     8 const { filter, map, merge, expand, pipe, stripListeners } = require("sdk/event/utils");
     9 const $ = require("./event/helpers");
    11 function isEven(x) !(x % 2)
    12 function inc(x) x + 1
    14 exports["test filter events"] = function(assert) {
    15   let input = {};
    16   let evens = filter(input, isEven);
    17   let actual = [];
    18   on(evens, "data", function(e) actual.push(e));
    20   [1, 2, 3, 4, 5, 6, 7].forEach(function(x) emit(input, "data", x));
    22   assert.deepEqual(actual, [2, 4, 6], "only even numbers passed through");
    23 };
    25 exports["test filter emits"] = $.emits(function(input, assert) {
    26   let output = filter(input, isEven);
    27   assert(output,  [1, 2, 3, 4, 5], [2, 4], "this is `output` & evens passed");
    28 });;
    30 exports["test filter reg once"] = $.registerOnce(function(input, assert) {
    31   assert(filter(input, isEven),  [1, 2, 3, 4, 5, 6], [2, 4, 6],
    32          "listener can be registered only once");
    33 });
    35 exports["test filter ignores new"] = $.ignoreNew(function(input, assert) {
    36   assert(filter(input, isEven), [1, 2, 3], [2],
    37          "new listener is ignored")
    38 });
    40 exports["test filter is FIFO"] = $.FIFO(function(input, assert) {
    41   assert(filter(input, isEven), [1, 2, 3, 4], [2, 4],
    42          "listeners are invoked in fifo order")
    43 });
    45 exports["test map events"] = function(assert) {
    46   let input = {};
    47   let incs = map(input, inc);
    48   let actual = [];
    49   on(incs, "data", function(e) actual.push(e));
    51   [1, 2, 3, 4].forEach(function(x) emit(input, "data", x));
    53   assert.deepEqual(actual, [2, 3, 4, 5], "all numbers were incremented");
    54 };
    56 exports["test map emits"] = $.emits(function(input, assert) {
    57   let output = map(input, inc);
    58   assert(output,  [1, 2, 3], [2, 3, 4], "this is `output` & evens passed");
    59 });;
    61 exports["test map reg once"] = $.registerOnce(function(input, assert) {
    62   assert(map(input, inc),  [1, 2, 3], [2, 3, 4],
    63          "listener can be registered only once");
    64 });
    66 exports["test map ignores new"] = $.ignoreNew(function(input, assert) {
    67   assert(map(input, inc), [1], [2],
    68          "new listener is ignored")
    69 });
    71 exports["test map is FIFO"] = $.FIFO(function(input, assert) {
    72   assert(map(input, inc), [1, 2, 3, 4], [2, 3, 4, 5],
    73          "listeners are invoked in fifo order")
    74 });
    76 exports["test merge stream[stream]"] = function(assert) {
    77   let a = {}, b = {}, c = {};
    78   let inputs = {};
    79   let actual = [];
    81   on(merge(inputs), "data", function($) actual.push($))
    83   emit(inputs, "data", a);
    84   emit(a, "data", "a1");
    85   emit(inputs, "data", b);
    86   emit(b, "data", "b1");
    87   emit(a, "data", "a2");
    88   emit(inputs, "data", c);
    89   emit(c, "data", "c1");
    90   emit(c, "data", "c2");
    91   emit(b, "data", "b2");
    92   emit(a, "data", "a3");
    94   assert.deepEqual(actual, ["a1", "b1", "a2", "c1", "c2", "b2", "a3"],
    95                    "all inputs data merged into one");
    96 };
    98 exports["test merge array[stream]"] = function(assert) {
    99   let a = {}, b = {}, c = {};
   100   let inputs = {};
   101   let actual = [];
   103   on(merge([a, b, c]), "data", function($) actual.push($))
   105   emit(a, "data", "a1");
   106   emit(b, "data", "b1");
   107   emit(a, "data", "a2");
   108   emit(c, "data", "c1");
   109   emit(c, "data", "c2");
   110   emit(b, "data", "b2");
   111   emit(a, "data", "a3");
   113   assert.deepEqual(actual, ["a1", "b1", "a2", "c1", "c2", "b2", "a3"],
   114                    "all inputs data merged into one");
   115 };
   117 exports["test merge emits"] = $.emits(function(input, assert) {
   118   let evens = filter(input, isEven)
   119   let output = merge([evens, input]);
   120   assert(output, [1, 2, 3], [1, 2, 2, 3], "this is `output` & evens passed");
   121 });
   124 exports["test merge reg once"] = $.registerOnce(function(input, assert) {
   125   let evens = filter(input, isEven)
   126   let output = merge([input, evens]);
   127   assert(output,  [1, 2, 3, 4], [1, 2, 2, 3, 4, 4],
   128          "listener can be registered only once");
   129 });
   131 exports["test merge ignores new"] = $.ignoreNew(function(input, assert) {
   132   let evens = filter(input, isEven)
   133   let output = merge([input, evens])
   134   assert(output, [1], [1],
   135          "new listener is ignored")
   136 });
   138 exports["test marge is FIFO"] = $.FIFO(function(input, assert) {
   139   let evens = filter(input, isEven)
   140   let output = merge([input, evens])
   142   assert(output, [1, 2, 3, 4], [1, 2, 2, 3, 4, 4],
   143          "listeners are invoked in fifo order")
   144 });
   146 exports["test expand"] = function(assert) {
   147   let a = {}, b = {}, c = {};
   148   let inputs = {};
   149   let actual = [];
   151   on(expand(inputs, function($) $()), "data", function($) actual.push($))
   153   emit(inputs, "data", function() a);
   154   emit(a, "data", "a1");
   155   emit(inputs, "data", function() b);
   156   emit(b, "data", "b1");
   157   emit(a, "data", "a2");
   158   emit(inputs, "data", function() c);
   159   emit(c, "data", "c1");
   160   emit(c, "data", "c2");
   161   emit(b, "data", "b2");
   162   emit(a, "data", "a3");
   164   assert.deepEqual(actual, ["a1", "b1", "a2", "c1", "c2", "b2", "a3"],
   165                    "all inputs data merged into one");
   166 };
   168 exports["test pipe"] = function (assert, done) {
   169   let src = {};
   170   let dest = {};
   172   let aneventCount = 0, multiargsCount = 0;
   173   let wildcardCount = {};
   175   on(dest, 'an-event', arg => {
   176     assert.equal(arg, 'my-arg', 'piped argument to event');
   177     ++aneventCount;
   178     check();
   179   });
   180   on(dest, 'multiargs', (...data) => {
   181     assert.equal(data[0], 'a', 'multiple arguments passed via pipe');
   182     assert.equal(data[1], 'b', 'multiple arguments passed via pipe');
   183     assert.equal(data[2], 'c', 'multiple arguments passed via pipe');
   184     ++multiargsCount;
   185     check();
   186   });
   188   on(dest, '*', (name, ...data) => {
   189     wildcardCount[name] = (wildcardCount[name] || 0) + 1;
   190     if (name === 'multiargs') {
   191       assert.equal(data[0], 'a', 'multiple arguments passed via pipe, wildcard');
   192       assert.equal(data[1], 'b', 'multiple arguments passed via pipe, wildcard');
   193       assert.equal(data[2], 'c', 'multiple arguments passed via pipe, wildcard');
   194     }
   195     if (name === 'an-event')
   196       assert.equal(data[0], 'my-arg', 'argument passed via pipe, wildcard');
   197     check();
   198   });
   200   pipe(src, dest);
   202   for (let i = 0; i < 3; i++)
   203     emit(src, 'an-event', 'my-arg');
   205   emit(src, 'multiargs', 'a', 'b', 'c');
   207   function check () {
   208     if (aneventCount === 3 && multiargsCount === 1 &&
   209         wildcardCount['an-event'] === 3 &&
   210         wildcardCount['multiargs'] === 1)
   211       done();
   212   }
   213 };
   215 exports["test pipe multiple targets"] = function (assert) {
   216   let src1 = {};
   217   let src2 = {};
   218   let middle = {};
   219   let dest = {};
   221   pipe(src1, middle);
   222   pipe(src2, middle);
   223   pipe(middle, dest);
   225   let middleFired = 0;
   226   let destFired = 0;
   227   let src1Fired = 0;
   228   let src2Fired = 0;
   230   on(src1, '*', () => src1Fired++);
   231   on(src2, '*', () => src2Fired++);
   232   on(middle, '*', () => middleFired++);
   233   on(dest, '*', () => destFired++);
   235   emit(src1, 'ev');
   236   assert.equal(src1Fired, 1, 'event triggers in source in pipe chain');
   237   assert.equal(middleFired, 1, 'event passes through the middle of pipe chain');
   238   assert.equal(destFired, 1, 'event propagates to end of pipe chain');
   239   assert.equal(src2Fired, 0, 'event does not fire on alternative chain routes');
   241   emit(src2, 'ev');
   242   assert.equal(src2Fired, 1, 'event triggers in source in pipe chain');
   243   assert.equal(middleFired, 2,
   244     'event passes through the middle of pipe chain from different src');
   245   assert.equal(destFired, 2,
   246     'event propagates to end of pipe chain from different src');
   247   assert.equal(src1Fired, 1, 'event does not fire on alternative chain routes');
   249   emit(middle, 'ev');
   250   assert.equal(middleFired, 3,
   251     'event triggers in source of pipe chain');
   252   assert.equal(destFired, 3,
   253     'event propagates to end of pipe chain from middle src');
   254   assert.equal(src1Fired, 1, 'event does not fire on alternative chain routes');
   255   assert.equal(src2Fired, 1, 'event does not fire on alternative chain routes');
   256 };
   258 exports['test stripListeners'] = function (assert) {
   259   var options = {
   260     onAnEvent: noop1,
   261     onMessage: noop2,
   262     verb: noop1,
   263     value: 100
   264   };
   266   var stripped = stripListeners(options);
   267   assert.ok(stripped !== options, 'stripListeners should return a new object');
   268   assert.equal(options.onAnEvent, noop1, 'stripListeners does not affect original');
   269   assert.equal(options.onMessage, noop2, 'stripListeners does not affect original');
   270   assert.equal(options.verb, noop1, 'stripListeners does not affect original');
   271   assert.equal(options.value, 100, 'stripListeners does not affect original');
   273   assert.ok(!stripped.onAnEvent, 'stripListeners removes `on*` values');
   274   assert.ok(!stripped.onMessage, 'stripListeners removes `on*` values');
   275   assert.equal(stripped.verb, noop1, 'stripListeners leaves not `on*` values');
   276   assert.equal(stripped.value, 100, 'stripListeners leaves not `on*` values');
   278   function noop1 () {}
   279   function noop2 () {}
   280 };
   282 require('test').run(exports);

mercurial