test-registry.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. exports['test:add'] = function(assert) {
  6. function Class() {}
  7. let fixture = require('sdk/util/registry').Registry(Class);
  8. let isAddEmitted = false;
  9. fixture.on('add', function(item) {
  10. assert.ok(
  11. item instanceof Class,
  12. 'if object added is not an instance should construct instance from it'
  13. );
  14. assert.ok(
  15. fixture.has(item),
  16. 'callback is called after instance is added'
  17. );
  18. assert.ok(
  19. !isAddEmitted,
  20. 'callback should be called for the same item only once'
  21. );
  22. isAddEmitted = true;
  23. });
  24. let object = fixture.add({});
  25. fixture.add(object);
  26. };
  27. exports['test:remove'] = function(assert) {
  28. function Class() {}
  29. let fixture = require('sdk/util/registry').Registry(Class);
  30. fixture.on('remove', function(item) {
  31. assert.ok(
  32. item instanceof Class,
  33. 'if object removed can be only instance of Class'
  34. );
  35. assert.ok(
  36. fixture.has(item),
  37. 'callback is called before instance is removed'
  38. );
  39. assert.ok(
  40. !isRemoveEmitted,
  41. 'callback should be called for the same item only once'
  42. );
  43. isRemoveEmitted = true;
  44. });
  45. fixture.remove({});
  46. let object = fixture.add({});
  47. fixture.remove(object);
  48. fixture.remove(object);
  49. };
  50. exports['test:items'] = function(assert) {
  51. function Class() {}
  52. let fixture = require('sdk/util/registry').Registry(Class),
  53. actual,
  54. times = 0;
  55. function testItem(item) {
  56. times ++;
  57. assert.equal(
  58. actual,
  59. item,
  60. 'item should match actual item being added/removed'
  61. );
  62. }
  63. actual = fixture.add({});
  64. fixture.on('add', testItem);
  65. fixture.on('remove', testItem);
  66. fixture.remove(actual);
  67. fixture.remove(fixture.add(actual = new Class()));
  68. assert.equal(3, times, 'should notify listeners on each call');
  69. };
  70. require('sdk/test').run(exports);