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: michael@0: 'use strict'; michael@0: michael@0: exports['test:add'] = function(assert) { michael@0: function Class() {} michael@0: let fixture = require('sdk/util/registry').Registry(Class); michael@0: let isAddEmitted = false; michael@0: fixture.on('add', function(item) { michael@0: assert.ok( michael@0: item instanceof Class, michael@0: 'if object added is not an instance should construct instance from it' michael@0: ); michael@0: assert.ok( michael@0: fixture.has(item), michael@0: 'callback is called after instance is added' michael@0: ); michael@0: assert.ok( michael@0: !isAddEmitted, michael@0: 'callback should be called for the same item only once' michael@0: ); michael@0: isAddEmitted = true; michael@0: }); michael@0: michael@0: let object = fixture.add({}); michael@0: fixture.add(object); michael@0: }; michael@0: michael@0: exports['test:remove'] = function(assert) { michael@0: function Class() {} michael@0: let fixture = require('sdk/util/registry').Registry(Class); michael@0: fixture.on('remove', function(item) { michael@0: assert.ok( michael@0: item instanceof Class, michael@0: 'if object removed can be only instance of Class' michael@0: ); michael@0: assert.ok( michael@0: fixture.has(item), michael@0: 'callback is called before instance is removed' michael@0: ); michael@0: assert.ok( michael@0: !isRemoveEmitted, michael@0: 'callback should be called for the same item only once' michael@0: ); michael@0: isRemoveEmitted = true; michael@0: }); michael@0: michael@0: fixture.remove({}); michael@0: let object = fixture.add({}); michael@0: fixture.remove(object); michael@0: fixture.remove(object); michael@0: }; michael@0: michael@0: exports['test:items'] = function(assert) { michael@0: function Class() {} michael@0: let fixture = require('sdk/util/registry').Registry(Class), michael@0: actual, michael@0: times = 0; michael@0: michael@0: function testItem(item) { michael@0: times ++; michael@0: assert.equal( michael@0: actual, michael@0: item, michael@0: 'item should match actual item being added/removed' michael@0: ); michael@0: } michael@0: michael@0: actual = fixture.add({}); michael@0: michael@0: fixture.on('add', testItem); michael@0: fixture.on('remove', testItem); michael@0: michael@0: fixture.remove(actual); michael@0: fixture.remove(fixture.add(actual = new Class())); michael@0: assert.equal(3, times, 'should notify listeners on each call'); michael@0: }; michael@0: michael@0: require('sdk/test').run(exports); michael@0: