1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/test-registry.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,82 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +'use strict'; 1.9 + 1.10 +exports['test:add'] = function(assert) { 1.11 + function Class() {} 1.12 + let fixture = require('sdk/util/registry').Registry(Class); 1.13 + let isAddEmitted = false; 1.14 + fixture.on('add', function(item) { 1.15 + assert.ok( 1.16 + item instanceof Class, 1.17 + 'if object added is not an instance should construct instance from it' 1.18 + ); 1.19 + assert.ok( 1.20 + fixture.has(item), 1.21 + 'callback is called after instance is added' 1.22 + ); 1.23 + assert.ok( 1.24 + !isAddEmitted, 1.25 + 'callback should be called for the same item only once' 1.26 + ); 1.27 + isAddEmitted = true; 1.28 + }); 1.29 + 1.30 + let object = fixture.add({}); 1.31 + fixture.add(object); 1.32 +}; 1.33 + 1.34 +exports['test:remove'] = function(assert) { 1.35 + function Class() {} 1.36 + let fixture = require('sdk/util/registry').Registry(Class); 1.37 + fixture.on('remove', function(item) { 1.38 + assert.ok( 1.39 + item instanceof Class, 1.40 + 'if object removed can be only instance of Class' 1.41 + ); 1.42 + assert.ok( 1.43 + fixture.has(item), 1.44 + 'callback is called before instance is removed' 1.45 + ); 1.46 + assert.ok( 1.47 + !isRemoveEmitted, 1.48 + 'callback should be called for the same item only once' 1.49 + ); 1.50 + isRemoveEmitted = true; 1.51 + }); 1.52 + 1.53 + fixture.remove({}); 1.54 + let object = fixture.add({}); 1.55 + fixture.remove(object); 1.56 + fixture.remove(object); 1.57 +}; 1.58 + 1.59 +exports['test:items'] = function(assert) { 1.60 + function Class() {} 1.61 + let fixture = require('sdk/util/registry').Registry(Class), 1.62 + actual, 1.63 + times = 0; 1.64 + 1.65 + function testItem(item) { 1.66 + times ++; 1.67 + assert.equal( 1.68 + actual, 1.69 + item, 1.70 + 'item should match actual item being added/removed' 1.71 + ); 1.72 + } 1.73 + 1.74 + actual = fixture.add({}); 1.75 + 1.76 + fixture.on('add', testItem); 1.77 + fixture.on('remove', testItem); 1.78 + 1.79 + fixture.remove(actual); 1.80 + fixture.remove(fixture.add(actual = new Class())); 1.81 + assert.equal(3, times, 'should notify listeners on each call'); 1.82 +}; 1.83 + 1.84 +require('sdk/test').run(exports); 1.85 +