michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: 'use strict'; michael@0: michael@0: const { Cc, Ci } = require('chrome'); michael@0: const { defer, all } = require('sdk/core/promise'); michael@0: const { setTimeout } = require('sdk/timers'); michael@0: const { request, response } = require('sdk/addon/host'); michael@0: const { send } = require('sdk/addon/events'); michael@0: const { filter } = require('sdk/event/utils'); michael@0: const { on, emit, off } = require('sdk/event/core'); michael@0: michael@0: let stream = filter(request, (data) => /sdk-x-test/.test(data.event)); michael@0: michael@0: exports.testSend = function (assert, done) { michael@0: on(stream, 'data', handle); michael@0: send('sdk-x-test-simple', { title: 'my test data' }).then((data) => { michael@0: assert.equal(data.title, 'my response', 'response is handled'); michael@0: off(stream, 'data', handle); michael@0: done(); michael@0: }, (reason) => { michael@0: assert.fail('should not call reject'); michael@0: }); michael@0: function handle (e) { michael@0: assert.equal(e.event, 'sdk-x-test-simple', 'correct event name'); michael@0: assert.ok(e.id != null, 'message has an ID'); michael@0: assert.equal(e.data.title, 'my test data', 'serialized data passes'); michael@0: e.data.title = 'my response'; michael@0: emit(response, 'data', e); michael@0: } michael@0: }; michael@0: michael@0: exports.testSendError = function (assert, done) { michael@0: on(stream, 'data', handle); michael@0: send('sdk-x-test-error', { title: 'my test data' }).then((data) => { michael@0: assert.fail('should not call success'); michael@0: }, (reason) => { michael@0: assert.equal(reason, 'ErrorInfo', 'should reject with error/reason'); michael@0: off(stream, 'data', handle); michael@0: done(); michael@0: }); michael@0: function handle (e) { michael@0: e.error = 'ErrorInfo'; michael@0: emit(response, 'data', e); michael@0: } michael@0: }; michael@0: michael@0: exports.testMultipleSends = function (assert, done) { michael@0: let count = 0; michael@0: let ids = []; michael@0: on(stream, 'data', handle); michael@0: ['firefox', 'thunderbird', 'rust'].map(data => michael@0: send('sdk-x-test-multi', { data: data }).then(val => { michael@0: assert.ok(val === 'firefox' || val === 'rust', 'successful calls resolve correctly'); michael@0: if (++count === 3) { michael@0: off(stream, 'data', handle); michael@0: done(); michael@0: } michael@0: }, reason => { michael@0: assert.equal(reason.error, 'too blue', 'rejected calls are rejected'); michael@0: if (++count === 3) { michael@0: off(stream, 'data', handle); michael@0: done(); michael@0: } michael@0: })); michael@0: michael@0: function handle (e) { michael@0: if (e.data !== 'firefox' || e.data !== 'rust') michael@0: e.error = { data: e.data, error: 'too blue' }; michael@0: assert.ok(!~ids.indexOf(e.id), 'ID should be unique'); michael@0: assert.equal(e.event, 'sdk-x-test-multi', 'has event name'); michael@0: ids.push(e.id); michael@0: emit(response, 'data', e); michael@0: } michael@0: }; michael@0: michael@0: exports.testSerialization = function (assert, done) { michael@0: on(stream, 'data', handle); michael@0: let object = { title: 'my test data' }; michael@0: let resObject; michael@0: send('sdk-x-test-serialize', object).then(data => { michael@0: data.title = 'another title'; michael@0: assert.equal(object.title, 'my test data', 'original object not modified'); michael@0: assert.equal(resObject.title, 'new title', 'object passed by value from host'); michael@0: off(stream, 'data', handle); michael@0: done(); michael@0: }, (reason) => { michael@0: assert.fail('should not call reject'); michael@0: }); michael@0: function handle (e) { michael@0: e.data.title = 'new title'; michael@0: assert.equal(object.title, 'my test data', 'object passed by value to host'); michael@0: resObject = e.data; michael@0: emit(response, 'data', e); michael@0: } michael@0: }; michael@0: michael@0: require('test').run(exports);