addon-sdk/source/test/test-host-events.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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 const { Cc, Ci } = require('chrome');
michael@0 7 const { defer, all } = require('sdk/core/promise');
michael@0 8 const { setTimeout } = require('sdk/timers');
michael@0 9 const { request, response } = require('sdk/addon/host');
michael@0 10 const { send } = require('sdk/addon/events');
michael@0 11 const { filter } = require('sdk/event/utils');
michael@0 12 const { on, emit, off } = require('sdk/event/core');
michael@0 13
michael@0 14 let stream = filter(request, (data) => /sdk-x-test/.test(data.event));
michael@0 15
michael@0 16 exports.testSend = function (assert, done) {
michael@0 17 on(stream, 'data', handle);
michael@0 18 send('sdk-x-test-simple', { title: 'my test data' }).then((data) => {
michael@0 19 assert.equal(data.title, 'my response', 'response is handled');
michael@0 20 off(stream, 'data', handle);
michael@0 21 done();
michael@0 22 }, (reason) => {
michael@0 23 assert.fail('should not call reject');
michael@0 24 });
michael@0 25 function handle (e) {
michael@0 26 assert.equal(e.event, 'sdk-x-test-simple', 'correct event name');
michael@0 27 assert.ok(e.id != null, 'message has an ID');
michael@0 28 assert.equal(e.data.title, 'my test data', 'serialized data passes');
michael@0 29 e.data.title = 'my response';
michael@0 30 emit(response, 'data', e);
michael@0 31 }
michael@0 32 };
michael@0 33
michael@0 34 exports.testSendError = function (assert, done) {
michael@0 35 on(stream, 'data', handle);
michael@0 36 send('sdk-x-test-error', { title: 'my test data' }).then((data) => {
michael@0 37 assert.fail('should not call success');
michael@0 38 }, (reason) => {
michael@0 39 assert.equal(reason, 'ErrorInfo', 'should reject with error/reason');
michael@0 40 off(stream, 'data', handle);
michael@0 41 done();
michael@0 42 });
michael@0 43 function handle (e) {
michael@0 44 e.error = 'ErrorInfo';
michael@0 45 emit(response, 'data', e);
michael@0 46 }
michael@0 47 };
michael@0 48
michael@0 49 exports.testMultipleSends = function (assert, done) {
michael@0 50 let count = 0;
michael@0 51 let ids = [];
michael@0 52 on(stream, 'data', handle);
michael@0 53 ['firefox', 'thunderbird', 'rust'].map(data =>
michael@0 54 send('sdk-x-test-multi', { data: data }).then(val => {
michael@0 55 assert.ok(val === 'firefox' || val === 'rust', 'successful calls resolve correctly');
michael@0 56 if (++count === 3) {
michael@0 57 off(stream, 'data', handle);
michael@0 58 done();
michael@0 59 }
michael@0 60 }, reason => {
michael@0 61 assert.equal(reason.error, 'too blue', 'rejected calls are rejected');
michael@0 62 if (++count === 3) {
michael@0 63 off(stream, 'data', handle);
michael@0 64 done();
michael@0 65 }
michael@0 66 }));
michael@0 67
michael@0 68 function handle (e) {
michael@0 69 if (e.data !== 'firefox' || e.data !== 'rust')
michael@0 70 e.error = { data: e.data, error: 'too blue' };
michael@0 71 assert.ok(!~ids.indexOf(e.id), 'ID should be unique');
michael@0 72 assert.equal(e.event, 'sdk-x-test-multi', 'has event name');
michael@0 73 ids.push(e.id);
michael@0 74 emit(response, 'data', e);
michael@0 75 }
michael@0 76 };
michael@0 77
michael@0 78 exports.testSerialization = function (assert, done) {
michael@0 79 on(stream, 'data', handle);
michael@0 80 let object = { title: 'my test data' };
michael@0 81 let resObject;
michael@0 82 send('sdk-x-test-serialize', object).then(data => {
michael@0 83 data.title = 'another title';
michael@0 84 assert.equal(object.title, 'my test data', 'original object not modified');
michael@0 85 assert.equal(resObject.title, 'new title', 'object passed by value from host');
michael@0 86 off(stream, 'data', handle);
michael@0 87 done();
michael@0 88 }, (reason) => {
michael@0 89 assert.fail('should not call reject');
michael@0 90 });
michael@0 91 function handle (e) {
michael@0 92 e.data.title = 'new title';
michael@0 93 assert.equal(object.title, 'my test data', 'object passed by value to host');
michael@0 94 resObject = e.data;
michael@0 95 emit(response, 'data', e);
michael@0 96 }
michael@0 97 };
michael@0 98
michael@0 99 require('test').run(exports);

mercurial