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.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 "use strict";
michael@0 5
michael@0 6
michael@0 7 const { id: addonID, name: addonName } = require("sdk/self");
michael@0 8 const { Cc, Ci, Cu } = require("chrome");
michael@0 9 const { Loader, LoaderWithHookedConsole2 } = require("sdk/test/loader");
michael@0 10 const { InputPort } = require("sdk/input/system");
michael@0 11 const { OutputPort } = require("sdk/output/system");
michael@0 12
michael@0 13 const { removeObserver, addObserver,
michael@0 14 notifyObservers } = Cc["@mozilla.org/observer-service;1"].
michael@0 15 getService(Ci.nsIObserverService);
michael@0 16
michael@0 17 const { lift, start, stop, send } = require("sdk/event/utils");
michael@0 18
michael@0 19 const isConsoleEvent = topic =>
michael@0 20 ["console-api-log-event",
michael@0 21 "console-storage-cache-event"].indexOf(topic) >= 0;
michael@0 22
michael@0 23 const message = x => ({wrappedJSObject: {data: x}});
michael@0 24
michael@0 25 exports["test start / stop ports"] = assert => {
michael@0 26 const input = new InputPort({ id: Date.now().toString(32), initial: {data:0} });
michael@0 27 const topic = input.topic;
michael@0 28
michael@0 29 assert.ok(topic.contains(addonID), "topics are namespaced to add-on");
michael@0 30
michael@0 31 const xs = lift(({data}) => "x:" + data, input);
michael@0 32 const ys = lift(({data}) => "y:" + data, input);
michael@0 33
michael@0 34 assert.deepEqual(input.value, {data:0}, "initila value is set");
michael@0 35 assert.deepEqual(xs.value, "x:0", "initial value is mapped");
michael@0 36 assert.deepEqual(ys.value, "y:0", "initial value is mapped");
michael@0 37
michael@0 38 notifyObservers(message(1), topic, null);
michael@0 39
michael@0 40 assert.deepEqual(input.value, {data:0}, "no message received on input port");
michael@0 41 assert.deepEqual(xs.value, "x:0", "no message received on xs");
michael@0 42 assert.deepEqual(ys.value, "y:0", "no message received on ys");
michael@0 43
michael@0 44 start(xs);
michael@0 45
michael@0 46
michael@0 47 notifyObservers(message(2), topic, null);
michael@0 48
michael@0 49 assert.deepEqual(input.value, {data:2}, "message received on input port");
michael@0 50 assert.deepEqual(xs.value, "x:2", "message received on xs");
michael@0 51 assert.deepEqual(ys.value, "y:2", "no message received on (not started) ys");
michael@0 52
michael@0 53
michael@0 54 notifyObservers(message(3), topic, null);
michael@0 55
michael@0 56
michael@0 57 assert.deepEqual(input.value, {data:3}, "message received on input port");
michael@0 58 assert.deepEqual(xs.value, "x:3", "message received on xs");
michael@0 59 assert.deepEqual(ys.value, "y:3", "message received on ys");
michael@0 60
michael@0 61
michael@0 62 notifyObservers(message(4), topic, null);
michael@0 63
michael@0 64 assert.deepEqual(input.value, {data:4}, "message received on input port");
michael@0 65 assert.deepEqual(xs.value, "x:4", "message not received on (stopped) xs");
michael@0 66 assert.deepEqual(ys.value, "y:4", "message received on ys");
michael@0 67
michael@0 68
michael@0 69 stop(input);
michael@0 70
michael@0 71 notifyObservers(message(5), topic, null);
michael@0 72
michael@0 73 assert.deepEqual(input.value, {data:4}, "message note received on input port");
michael@0 74 assert.deepEqual(xs.value, "x:4", "message not received on (stopped) xs");
michael@0 75 assert.deepEqual(ys.value, "y:4", "message not received on (stopped) ys");
michael@0 76 };
michael@0 77
michael@0 78 exports["test send messages to nsIObserverService"] = assert => {
michael@0 79 let messages = [];
michael@0 80
michael@0 81 const { newURI } = Cc['@mozilla.org/network/io-service;1'].
michael@0 82 getService(Ci.nsIIOService);
michael@0 83
michael@0 84 const output = new OutputPort({ id: Date.now().toString(32), sync: true });
michael@0 85 const topic = output.topic;
michael@0 86
michael@0 87 const observer = {
michael@0 88 QueryInterface: function() {
michael@0 89 return this;
michael@0 90 },
michael@0 91 observe: (subject, topic, data) => {
michael@0 92 // Ignores internal console events
michael@0 93 if (!isConsoleEvent(topic)) {
michael@0 94 messages.push({
michael@0 95 topic: topic,
michael@0 96 subject: subject
michael@0 97 });
michael@0 98 }
michael@0 99 }
michael@0 100 };
michael@0 101
michael@0 102 addObserver(observer, topic, false);
michael@0 103
michael@0 104 send(output, null);
michael@0 105 assert.deepEqual(messages.shift(), { topic: topic, subject: null },
michael@0 106 "null message received");
michael@0 107
michael@0 108
michael@0 109 const uri = newURI("http://www.foo.com", null, null);
michael@0 110 send(output, uri);
michael@0 111
michael@0 112 assert.deepEqual(messages.shift(), { topic: topic, subject: uri },
michael@0 113 "message received");
michael@0 114
michael@0 115
michael@0 116 function customSubject() {}
michael@0 117 send(output, customSubject);
michael@0 118
michael@0 119 let message = messages.shift();
michael@0 120 assert.equal(message.topic, topic, "topic was received");
michael@0 121 assert.equal(message.subject.wrappedJSObject, customSubject,
michael@0 122 "custom subject is received");
michael@0 123
michael@0 124 removeObserver(observer, topic);
michael@0 125
michael@0 126 send(output, { data: "more data" });
michael@0 127
michael@0 128 assert.deepEqual(messages, [],
michael@0 129 "no more data received");
michael@0 130
michael@0 131 addObserver(observer, "*", false);
michael@0 132
michael@0 133 send(output, { data: "data again" });
michael@0 134
michael@0 135 message = messages.shift();
michael@0 136 assert.equal(message.topic, topic, "topic was received");
michael@0 137 assert.deepEqual(message.subject.wrappedJSObject,
michael@0 138 { data: "data again" },
michael@0 139 "wrapped message received");
michael@0 140
michael@0 141 removeObserver(observer, "*");
michael@0 142
michael@0 143 send(output, { data: "last data" });
michael@0 144 assert.deepEqual(messages, [],
michael@0 145 "no more data received");
michael@0 146
michael@0 147 assert.throws(() => send(output, "hi"),
michael@0 148 /Unsupproted message type: `string`/,
michael@0 149 "strings can't be send");
michael@0 150
michael@0 151 assert.throws(() => send(output, 4),
michael@0 152 /Unsupproted message type: `number`/,
michael@0 153 "numbers can't be send");
michael@0 154
michael@0 155 assert.throws(() => send(output, void(0)),
michael@0 156 /Unsupproted message type: `undefined`/,
michael@0 157 "undefineds can't be send");
michael@0 158
michael@0 159 assert.throws(() => send(output, true),
michael@0 160 /Unsupproted message type: `boolean`/,
michael@0 161 "booleans can't be send");
michael@0 162 };
michael@0 163
michael@0 164 exports["test async OutputPort"] = (assert, done) => {
michael@0 165 let async = false;
michael@0 166 const output = new OutputPort({ id: Date.now().toString(32) });
michael@0 167 const observer = {
michael@0 168 observe: (subject, topic, data) => {
michael@0 169 removeObserver(observer, topic);
michael@0 170 assert.equal(topic, output.topic, "correct topic");
michael@0 171 assert.deepEqual(subject.wrappedJSObject, {foo: "bar"}, "message received");
michael@0 172 assert.ok(async, "message received async");
michael@0 173 done();
michael@0 174 }
michael@0 175 };
michael@0 176 addObserver(observer, output.topic, false);
michael@0 177 send(output, {foo: "bar"});
michael@0 178
michael@0 179 assert.throws(() => send(output, "boom"), "can only send object");
michael@0 180 async = true;
michael@0 181 };
michael@0 182
michael@0 183 exports["test explicit output topic"] = (assert, done) => {
michael@0 184 const topic = Date.now().toString(32);
michael@0 185 const output = new OutputPort({ topic: topic });
michael@0 186 const observer = {
michael@0 187 observe: (subject, topic, data) => {
michael@0 188 removeObserver(observer, topic);
michael@0 189 assert.deepEqual(subject.wrappedJSObject, {foo: "bar"}, "message received");
michael@0 190 done();
michael@0 191 }
michael@0 192 };
michael@0 193
michael@0 194 assert.equal(output.topic, topic, "given topic is used");
michael@0 195
michael@0 196 addObserver(observer, topic, false);
michael@0 197 send(output, {foo: "bar"});
michael@0 198 };
michael@0 199
michael@0 200 exports["test explicit input topic"] = (assert) => {
michael@0 201 const topic = Date.now().toString(32);
michael@0 202 const input = new InputPort({ topic: topic });
michael@0 203
michael@0 204 start(input);
michael@0 205 assert.equal(input.topic, topic, "given topic is used");
michael@0 206
michael@0 207
michael@0 208 notifyObservers({wrappedJSObject: {foo: "bar"}}, topic, null);
michael@0 209
michael@0 210 assert.deepEqual(input.value, {foo: "bar"}, "message received");
michael@0 211 };
michael@0 212
michael@0 213
michael@0 214 exports["test receive what was send"] = assert => {
michael@0 215 const id = Date.now().toString(32);
michael@0 216 const input = new InputPort({ id: id, initial: 0});
michael@0 217 const output = new OutputPort({ id: id, sync: true });
michael@0 218
michael@0 219 assert.ok(input.topic.contains(addonID),
michael@0 220 "input topic is namespaced to addon");
michael@0 221 assert.equal(input.topic, output.topic,
michael@0 222 "input & output get same topics from id.");
michael@0 223
michael@0 224 start(input);
michael@0 225
michael@0 226 assert.equal(input.value, 0, "initial value is set");
michael@0 227
michael@0 228 send(output, { data: 1 });
michael@0 229 assert.deepEqual(input.value, {data: 1}, "message unwrapped");
michael@0 230
michael@0 231 send(output, []);
michael@0 232 assert.deepEqual(input.value, [], "array message unwrapped");
michael@0 233
michael@0 234 send(output, null);
michael@0 235 assert.deepEqual(input.value, null, "null message received");
michael@0 236
michael@0 237 send(output, new String("message"));
michael@0 238 assert.deepEqual(input.value, new String("message"),
michael@0 239 "string instance received");
michael@0 240
michael@0 241 send(output, /pattern/);
michael@0 242 assert.deepEqual(input.value, /pattern/, "regexp received");
michael@0 243
michael@0 244 assert.throws(() => send(output, "hi"),
michael@0 245 /Unsupproted message type: `string`/,
michael@0 246 "strings can't be send");
michael@0 247
michael@0 248 assert.throws(() => send(output, 4),
michael@0 249 /Unsupproted message type: `number`/,
michael@0 250 "numbers can't be send");
michael@0 251
michael@0 252 assert.throws(() => send(output, void(0)),
michael@0 253 /Unsupproted message type: `undefined`/,
michael@0 254 "undefineds can't be send");
michael@0 255
michael@0 256 assert.throws(() => send(output, true),
michael@0 257 /Unsupproted message type: `boolean`/,
michael@0 258 "booleans can't be send");
michael@0 259
michael@0 260 stop(input);
michael@0 261 };
michael@0 262
michael@0 263
michael@0 264 exports["-test error reporting"] = function(assert) {
michael@0 265 let { loader, messages } = LoaderWithHookedConsole2(module);
michael@0 266 const { start, stop, lift } = loader.require("sdk/event/utils");
michael@0 267 const { InputPort } = loader.require("sdk/input/system");
michael@0 268 const { OutputPort } = loader.require("sdk/output/system");
michael@0 269 const id = "error:" + Date.now().toString(32);
michael@0 270
michael@0 271 const raise = x => { if (x) throw new Error("foo"); };
michael@0 272
michael@0 273 const input = new InputPort({ id: id });
michael@0 274 const output = new OutputPort({ id: id, sync: true });
michael@0 275 const xs = lift(raise, input);
michael@0 276
michael@0 277 assert.equal(input.value, null, "initial inherited");
michael@0 278
michael@0 279 send(output, { data: "yo yo" });
michael@0 280
michael@0 281 assert.deepEqual(messages, [], "nothing happend yet");
michael@0 282
michael@0 283 start(xs);
michael@0 284
michael@0 285 send(output, { data: "first" });
michael@0 286
michael@0 287 assert.equal(messages.length, 4, "Got an exception");
michael@0 288
michael@0 289
michael@0 290 assert.equal(messages[0], "console.error: " + addonName + ": \n",
michael@0 291 "error is logged");
michael@0 292
michael@0 293 assert.ok(/Unhandled error/.test(messages[1]),
michael@0 294 "expected error message");
michael@0 295
michael@0 296 loader.unload();
michael@0 297 };
michael@0 298
michael@0 299 exports["test unload ends input port"] = assert => {
michael@0 300 const loader = Loader(module);
michael@0 301 const { start, stop, lift } = loader.require("sdk/event/utils");
michael@0 302 const { InputPort } = loader.require("sdk/input/system");
michael@0 303
michael@0 304 const id = "unload!" + Date.now().toString(32);
michael@0 305 const input = new InputPort({ id: id });
michael@0 306
michael@0 307 start(input);
michael@0 308 notifyObservers(message(1), input.topic, null);
michael@0 309 assert.deepEqual(input.value, {data: 1}, "message received");
michael@0 310
michael@0 311 notifyObservers(message(2), input.topic, null);
michael@0 312 assert.deepEqual(input.value, {data: 2}, "message received");
michael@0 313
michael@0 314 loader.unload();
michael@0 315 notifyObservers(message(3), input.topic, null);
michael@0 316 assert.deepEqual(input.value, {data: 2}, "message wasn't received");
michael@0 317 };
michael@0 318
michael@0 319 require("sdk/test").run(exports);

mercurial