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

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

mercurial