test-host-events.js 3.2 KB

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