addon-sdk/source/test/test-system-input-output.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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 "use strict";
     7 const { id: addonID, name: addonName } = require("sdk/self");
     8 const { Cc, Ci, Cu } = require("chrome");
     9 const { Loader, LoaderWithHookedConsole2 } = require("sdk/test/loader");
    10 const { InputPort } = require("sdk/input/system");
    11 const { OutputPort } = require("sdk/output/system");
    13 const { removeObserver, addObserver,
    14         notifyObservers } = Cc["@mozilla.org/observer-service;1"].
    15                               getService(Ci.nsIObserverService);
    17 const { lift, start, stop, send } = require("sdk/event/utils");
    19 const isConsoleEvent = topic =>
    20   ["console-api-log-event",
    21    "console-storage-cache-event"].indexOf(topic) >= 0;
    23 const message = x => ({wrappedJSObject: {data: x}});
    25 exports["test start / stop ports"] = assert => {
    26   const input = new InputPort({ id: Date.now().toString(32), initial: {data:0} });
    27   const topic = input.topic;
    29   assert.ok(topic.contains(addonID), "topics are namespaced to add-on");
    31   const xs = lift(({data}) => "x:" + data, input);
    32   const ys = lift(({data}) => "y:" + data, input);
    34   assert.deepEqual(input.value, {data:0}, "initila value is set");
    35   assert.deepEqual(xs.value, "x:0", "initial value is mapped");
    36   assert.deepEqual(ys.value, "y:0", "initial value is mapped");
    38   notifyObservers(message(1), topic, null);
    40   assert.deepEqual(input.value, {data:0}, "no message received on input port");
    41   assert.deepEqual(xs.value, "x:0", "no message received on xs");
    42   assert.deepEqual(ys.value, "y:0", "no message received on ys");
    44   start(xs);
    47   notifyObservers(message(2), topic, null);
    49   assert.deepEqual(input.value, {data:2}, "message received on input port");
    50   assert.deepEqual(xs.value, "x:2", "message received on xs");
    51   assert.deepEqual(ys.value, "y:2", "no message received on (not started) ys");
    54   notifyObservers(message(3), topic, null);
    57   assert.deepEqual(input.value, {data:3}, "message received on input port");
    58   assert.deepEqual(xs.value, "x:3", "message received on xs");
    59   assert.deepEqual(ys.value, "y:3", "message received on ys");
    62   notifyObservers(message(4), topic, null);
    64   assert.deepEqual(input.value, {data:4}, "message received on input port");
    65   assert.deepEqual(xs.value, "x:4", "message not received on (stopped) xs");
    66   assert.deepEqual(ys.value, "y:4", "message received on ys");
    69   stop(input);
    71   notifyObservers(message(5), topic, null);
    73   assert.deepEqual(input.value, {data:4}, "message note received on input port");
    74   assert.deepEqual(xs.value, "x:4", "message not received on (stopped) xs");
    75   assert.deepEqual(ys.value, "y:4", "message not received on (stopped) ys");
    76 };
    78 exports["test send messages to nsIObserverService"] = assert => {
    79   let messages = [];
    81   const { newURI } = Cc['@mozilla.org/network/io-service;1'].
    82                        getService(Ci.nsIIOService);
    84   const output = new OutputPort({ id: Date.now().toString(32), sync: true });
    85   const topic = output.topic;
    87   const observer = {
    88     QueryInterface: function() {
    89       return this;
    90     },
    91     observe: (subject, topic, data) => {
    92       // Ignores internal console events
    93       if (!isConsoleEvent(topic)) {
    94         messages.push({
    95           topic: topic,
    96           subject: subject
    97         });
    98       }
    99     }
   100   };
   102   addObserver(observer, topic, false);
   104   send(output, null);
   105   assert.deepEqual(messages.shift(), { topic: topic, subject: null },
   106                    "null message received");
   109   const uri = newURI("http://www.foo.com", null, null);
   110   send(output, uri);
   112   assert.deepEqual(messages.shift(), { topic: topic, subject: uri },
   113                    "message received");
   116   function customSubject() {}
   117   send(output, customSubject);
   119   let message = messages.shift();
   120   assert.equal(message.topic, topic, "topic was received");
   121   assert.equal(message.subject.wrappedJSObject, customSubject,
   122                "custom subject is received");
   124   removeObserver(observer, topic);
   126   send(output, { data: "more data" });
   128   assert.deepEqual(messages, [],
   129                    "no more data received");
   131   addObserver(observer, "*", false);
   133   send(output, { data: "data again" });
   135   message = messages.shift();
   136   assert.equal(message.topic, topic, "topic was received");
   137   assert.deepEqual(message.subject.wrappedJSObject,
   138                    { data: "data again" },
   139                    "wrapped message received");
   141   removeObserver(observer, "*");
   143   send(output, { data: "last data" });
   144   assert.deepEqual(messages, [],
   145                    "no more data received");
   147   assert.throws(() => send(output, "hi"),
   148                 /Unsupproted message type: `string`/,
   149                 "strings can't be send");
   151   assert.throws(() => send(output, 4),
   152                 /Unsupproted message type: `number`/,
   153                 "numbers can't be send");
   155   assert.throws(() => send(output, void(0)),
   156                 /Unsupproted message type: `undefined`/,
   157                 "undefineds can't be send");
   159   assert.throws(() => send(output, true),
   160                 /Unsupproted message type: `boolean`/,
   161                 "booleans can't be send");
   162 };
   164 exports["test async OutputPort"] = (assert, done) => {
   165   let async = false;
   166   const output = new OutputPort({ id: Date.now().toString(32) });
   167   const observer = {
   168     observe: (subject, topic, data) => {
   169       removeObserver(observer, topic);
   170       assert.equal(topic, output.topic, "correct topic");
   171       assert.deepEqual(subject.wrappedJSObject, {foo: "bar"}, "message received");
   172       assert.ok(async, "message received async");
   173       done();
   174     }
   175   };
   176   addObserver(observer, output.topic, false);
   177   send(output, {foo: "bar"});
   179   assert.throws(() => send(output, "boom"), "can only send object");
   180   async = true;
   181 };
   183 exports["test explicit output topic"] = (assert, done) => {
   184   const topic = Date.now().toString(32);
   185   const output = new OutputPort({ topic: topic });
   186   const observer = {
   187     observe: (subject, topic, data) => {
   188       removeObserver(observer, topic);
   189       assert.deepEqual(subject.wrappedJSObject, {foo: "bar"}, "message received");
   190       done();
   191     }
   192   };
   194   assert.equal(output.topic, topic, "given topic is used");
   196   addObserver(observer, topic, false);
   197   send(output, {foo: "bar"});
   198 };
   200 exports["test explicit input topic"] = (assert) => {
   201   const topic = Date.now().toString(32);
   202   const input = new InputPort({ topic: topic });
   204   start(input);
   205   assert.equal(input.topic, topic, "given topic is used");
   208   notifyObservers({wrappedJSObject: {foo: "bar"}}, topic, null);
   210   assert.deepEqual(input.value, {foo: "bar"}, "message received");
   211 };
   214 exports["test receive what was send"] = assert => {
   215   const id = Date.now().toString(32);
   216   const input = new InputPort({ id: id, initial: 0});
   217   const output = new OutputPort({ id: id, sync: true });
   219   assert.ok(input.topic.contains(addonID),
   220             "input topic is namespaced to addon");
   221   assert.equal(input.topic, output.topic,
   222               "input & output get same topics from id.");
   224   start(input);
   226   assert.equal(input.value, 0, "initial value is set");
   228   send(output, { data: 1 });
   229   assert.deepEqual(input.value, {data: 1}, "message unwrapped");
   231   send(output, []);
   232   assert.deepEqual(input.value, [], "array message unwrapped");
   234   send(output, null);
   235   assert.deepEqual(input.value, null, "null message received");
   237   send(output, new String("message"));
   238   assert.deepEqual(input.value, new String("message"),
   239                    "string instance received");
   241   send(output, /pattern/);
   242   assert.deepEqual(input.value, /pattern/, "regexp received");
   244   assert.throws(() => send(output, "hi"),
   245                 /Unsupproted message type: `string`/,
   246                 "strings can't be send");
   248   assert.throws(() => send(output, 4),
   249                 /Unsupproted message type: `number`/,
   250                 "numbers can't be send");
   252   assert.throws(() => send(output, void(0)),
   253                 /Unsupproted message type: `undefined`/,
   254                 "undefineds can't be send");
   256   assert.throws(() => send(output, true),
   257                 /Unsupproted message type: `boolean`/,
   258                 "booleans can't be send");
   260   stop(input);
   261 };
   264 exports["-test error reporting"] = function(assert) {
   265   let { loader, messages } = LoaderWithHookedConsole2(module);
   266   const { start, stop, lift } = loader.require("sdk/event/utils");
   267   const { InputPort } = loader.require("sdk/input/system");
   268   const { OutputPort } = loader.require("sdk/output/system");
   269   const id = "error:" + Date.now().toString(32);
   271   const raise = x => { if (x) throw new Error("foo"); };
   273   const input = new InputPort({ id: id });
   274   const output = new OutputPort({ id: id, sync: true });
   275   const xs = lift(raise, input);
   277   assert.equal(input.value, null, "initial inherited");
   279   send(output, { data: "yo yo" });
   281   assert.deepEqual(messages, [], "nothing happend yet");
   283   start(xs);
   285   send(output, { data: "first" });
   287   assert.equal(messages.length, 4, "Got an exception");
   290   assert.equal(messages[0], "console.error: " + addonName + ": \n",
   291                "error is logged");
   293   assert.ok(/Unhandled error/.test(messages[1]),
   294             "expected error message");
   296   loader.unload();
   297 };
   299 exports["test unload ends input port"] = assert => {
   300   const loader = Loader(module);
   301   const { start, stop, lift } = loader.require("sdk/event/utils");
   302   const { InputPort } = loader.require("sdk/input/system");
   304   const id = "unload!" + Date.now().toString(32);
   305   const input = new InputPort({ id: id });
   307   start(input);
   308   notifyObservers(message(1), input.topic, null);
   309   assert.deepEqual(input.value, {data: 1}, "message received");
   311   notifyObservers(message(2), input.topic, null);
   312   assert.deepEqual(input.value, {data: 2}, "message received");
   314   loader.unload();
   315   notifyObservers(message(3), input.topic, null);
   316   assert.deepEqual(input.value, {data: 2}, "message wasn't received");
   317 };
   319 require("sdk/test").run(exports);

mercurial