addon-sdk/source/test/test-registry.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 'use strict';
michael@0 6
michael@0 7 exports['test:add'] = function(assert) {
michael@0 8 function Class() {}
michael@0 9 let fixture = require('sdk/util/registry').Registry(Class);
michael@0 10 let isAddEmitted = false;
michael@0 11 fixture.on('add', function(item) {
michael@0 12 assert.ok(
michael@0 13 item instanceof Class,
michael@0 14 'if object added is not an instance should construct instance from it'
michael@0 15 );
michael@0 16 assert.ok(
michael@0 17 fixture.has(item),
michael@0 18 'callback is called after instance is added'
michael@0 19 );
michael@0 20 assert.ok(
michael@0 21 !isAddEmitted,
michael@0 22 'callback should be called for the same item only once'
michael@0 23 );
michael@0 24 isAddEmitted = true;
michael@0 25 });
michael@0 26
michael@0 27 let object = fixture.add({});
michael@0 28 fixture.add(object);
michael@0 29 };
michael@0 30
michael@0 31 exports['test:remove'] = function(assert) {
michael@0 32 function Class() {}
michael@0 33 let fixture = require('sdk/util/registry').Registry(Class);
michael@0 34 fixture.on('remove', function(item) {
michael@0 35 assert.ok(
michael@0 36 item instanceof Class,
michael@0 37 'if object removed can be only instance of Class'
michael@0 38 );
michael@0 39 assert.ok(
michael@0 40 fixture.has(item),
michael@0 41 'callback is called before instance is removed'
michael@0 42 );
michael@0 43 assert.ok(
michael@0 44 !isRemoveEmitted,
michael@0 45 'callback should be called for the same item only once'
michael@0 46 );
michael@0 47 isRemoveEmitted = true;
michael@0 48 });
michael@0 49
michael@0 50 fixture.remove({});
michael@0 51 let object = fixture.add({});
michael@0 52 fixture.remove(object);
michael@0 53 fixture.remove(object);
michael@0 54 };
michael@0 55
michael@0 56 exports['test:items'] = function(assert) {
michael@0 57 function Class() {}
michael@0 58 let fixture = require('sdk/util/registry').Registry(Class),
michael@0 59 actual,
michael@0 60 times = 0;
michael@0 61
michael@0 62 function testItem(item) {
michael@0 63 times ++;
michael@0 64 assert.equal(
michael@0 65 actual,
michael@0 66 item,
michael@0 67 'item should match actual item being added/removed'
michael@0 68 );
michael@0 69 }
michael@0 70
michael@0 71 actual = fixture.add({});
michael@0 72
michael@0 73 fixture.on('add', testItem);
michael@0 74 fixture.on('remove', testItem);
michael@0 75
michael@0 76 fixture.remove(actual);
michael@0 77 fixture.remove(fixture.add(actual = new Class()));
michael@0 78 assert.equal(3, times, 'should notify listeners on each call');
michael@0 79 };
michael@0 80
michael@0 81 require('sdk/test').run(exports);
michael@0 82

mercurial