addon-sdk/source/test/test-dispatcher.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/.
     4  */
     5 "use strict";
     7 const { dispatcher } = require("sdk/util/dispatcher");
     9 exports["test dispatcher API"] = assert => {
    10   const dispatch = dispatcher();
    12   assert.equal(typeof(dispatch), "function",
    13                "dispatch is a function");
    15   assert.equal(typeof(dispatch.define), "function",
    16                "dispatch.define is a function");
    18   assert.equal(typeof(dispatch.implement), "function",
    19                "dispatch.implement is a function");
    21   assert.equal(typeof(dispatch.when), "function",
    22                "dispatch.when is a function");
    23 };
    25 exports["test dispatcher"] = assert => {
    26   const isDuck = dispatcher();
    28   const quacks = x => x && typeof(x.quack) === "function";
    30   const Duck = function() {};
    31   const Goose = function() {};
    33   const True = _ => true;
    34   const False = _ => false;
    38   isDuck.define(Goose, False);
    39   isDuck.define(Duck, True);
    40   isDuck.when(quacks, True);
    42   assert.equal(isDuck(new Goose()), false,
    43                "Goose ain't duck");
    45   assert.equal(isDuck(new Duck()), true,
    46                "Ducks are ducks");
    48   assert.equal(isDuck({ quack: () => "Quaaaaaack!" }), true,
    49                "It's a duck if it quacks");
    52   assert.throws(() => isDuck({}), /Type does not implements method/, "not implemneted");
    54   isDuck.define(Object, False);
    56   assert.equal(isDuck({}), false,
    57                "Ain't duck if it does not quacks!");
    58 };
    60 exports["test redefining fails"] = assert => {
    61   const isPM = dispatcher();
    62   const isAfternoon = time => time.getHours() > 12;
    64   isPM.when(isAfternoon, _ => true);
    66   assert.equal(isPM(new Date(Date.parse("Jan 23, 1985, 13:20:00"))), true,
    67                "yeap afternoon");
    68   assert.equal(isPM({ getHours: _ => 17 }), true,
    69                 "seems like afternoon");
    71   assert.throws(() => isPM.when(isAfternoon, x => x > 12 && x < 24),
    72                 /Already implemented for the given predicate/,
    73                "can't redefine on same predicate");
    75 };
    77 require("sdk/test").run(exports);

mercurial