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