forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_entityreference.js
More file actions
396 lines (303 loc) · 15.7 KB
/
Copy pathtest_entityreference.js
File metadata and controls
396 lines (303 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
describe("pc.EntityReference", function () {
var app;
var testEntity;
var testComponent;
var otherEntity1;
var otherEntity2;
beforeEach(function () {
app = new pc.Application(document.createElement("canvas"));
app.systems.add(new pc.DummyComponentSystem(app));
testEntity = new pc.Entity("testEntity", app);
testComponent = testEntity.addComponent("dummy", {});
otherEntity1 = new pc.Entity("otherEntity1", app);
otherEntity1.addComponent("dummy", {});
otherEntity2 = new pc.Entity("otherEntity2", app);
app.root.addChild(testEntity);
app.root.addChild(otherEntity1);
app.root.addChild(otherEntity2);
});
afterEach(function () {
sinon.restore();
app.destroy();
});
// Assertion helpers that rely on checking some private state. Usually I wouldn't do
// this, but given that we're checking such a stable part of the API (_callbacks has
// been present since 2011) I think it's preferable to adding public methods to the
// Events class that are only required for tests. Also it's critical that listener
// addition and removal is implemented correctly by EntityReference in order to avoid
// memory leaks, so the benefits as significant.
function getTotalEventListeners(entity) {
var total = 0;
Object.keys(entity._callbacks || {}).forEach(function(eventName) {
total += entity._callbacks[eventName].length;
});
return total;
}
function getNumListenersForEvent(entity, eventName) {
return (entity._callbacks && entity._callbacks[eventName] && entity._callbacks[eventName].length) || 0;
}
it("provides a reference to the entity once the guid is populated", function () {
var reference = new pc.EntityReference(testComponent, "myEntity1");
expect(reference.entity).to.equal(null);
testComponent.myEntity1 = otherEntity1.getGuid();
expect(reference.entity).to.equal(otherEntity1);
});
it("does not attempt to resolve the entity reference if the parent component is not on the scene graph yet", function () {
app.root.removeChild(testEntity);
sinon.spy(app.root, "findByGuid");
var reference = new pc.EntityReference(testComponent, "myEntity1");
testComponent.myEntity1 = otherEntity1.getGuid();
expect(reference.entity).to.equal(null);
expect(app.root.findByGuid.callCount).to.equal(0);
});
it("resolves the entity reference when onParentComponentEnable() is called", function () {
app.root.removeChild(testEntity);
var reference = new pc.EntityReference(testComponent, "myEntity1");
testComponent.myEntity1 = otherEntity1.getGuid();
expect(reference.entity).to.equal(null);
app.root.addChild(testEntity);
reference.onParentComponentEnable();
expect(reference.entity).to.equal(otherEntity1);
});
it("nullifies the reference when the guid is nullified", function () {
var reference = new pc.EntityReference(testComponent, "myEntity1");
testComponent.myEntity1 = otherEntity1.getGuid();
expect(reference.entity).to.equal(otherEntity1);
testComponent.myEntity1 = null;
expect(reference.entity).to.equal(null);
});
it("nullifies the reference when the referenced entity is destroyed", function () {
var reference = new pc.EntityReference(testComponent, "myEntity1");
testComponent.myEntity1 = otherEntity1.getGuid();
expect(reference.entity).to.equal(otherEntity1);
otherEntity1.destroy();
expect(reference.entity).to.equal(null);
});
it("removes all entity and component listeners when the guid is reassigned", function () {
new pc.EntityReference(testComponent, "myEntity1", {
"entity#foo": sinon.stub(),
"dummy#bar": sinon.stub()
});
testComponent.myEntity1 = otherEntity1.getGuid();
expect(getTotalEventListeners(otherEntity1)).to.equal(2);
expect(getNumListenersForEvent(otherEntity1.dummy, 'bar')).to.equal(1);
testComponent.myEntity1 = otherEntity2.getGuid();
expect(getTotalEventListeners(otherEntity1)).to.equal(0);
expect(getNumListenersForEvent(otherEntity1.dummy, 'bar')).to.equal(0);
});
it("removes all entity and component listeners when the parent component is removed", function () {
new pc.EntityReference(testComponent, "myEntity1", {
"entity#foo": sinon.stub(),
"dummy#bar": sinon.stub()
});
testComponent.myEntity1 = otherEntity1.getGuid();
expect(getTotalEventListeners(otherEntity1)).to.equal(2);
expect(getNumListenersForEvent(otherEntity1.dummy, 'bar')).to.equal(1);
expect(getNumListenersForEvent(app.systems.dummy, 'add')).to.equal(1);
expect(getNumListenersForEvent(app.systems.dummy, 'beforeremove')).to.equal(2);
testEntity.removeComponent('dummy');
expect(getTotalEventListeners(otherEntity1)).to.equal(0);
expect(getNumListenersForEvent(otherEntity1.dummy, 'bar')).to.equal(0);
expect(getNumListenersForEvent(app.systems.dummy, 'add')).to.equal(0);
expect(getNumListenersForEvent(app.systems.dummy, 'beforeremove')).to.equal(0);
});
it("removes all entity and component listeners when the parent component's entity is destroyed", function () {
new pc.EntityReference(testComponent, "myEntity1", {
"entity#foo": sinon.stub(),
"dummy#bar": sinon.stub()
});
testComponent.myEntity1 = otherEntity1.getGuid();
expect(getTotalEventListeners(otherEntity1)).to.equal(2);
expect(getNumListenersForEvent(otherEntity1.dummy, 'bar')).to.equal(1);
expect(getNumListenersForEvent(app.systems.dummy, 'add')).to.equal(1);
expect(getNumListenersForEvent(app.systems.dummy, 'beforeremove')).to.equal(2);
testEntity.destroy();
expect(getTotalEventListeners(otherEntity1)).to.equal(0);
expect(getNumListenersForEvent(otherEntity1.dummy, 'bar')).to.equal(0);
expect(getNumListenersForEvent(app.systems.dummy, 'add')).to.equal(0);
expect(getNumListenersForEvent(app.systems.dummy, 'beforeremove')).to.equal(0);
});
it("fires component gain events when a guid is first assigned, if the referenced entity already has the component", function () {
var gainListener = sinon.stub();
new pc.EntityReference(testComponent, "myEntity1", {
"dummy#gain": gainListener
});
testComponent.myEntity1 = otherEntity1.getGuid();
expect(gainListener.callCount).to.equal(1);
});
it("fires component gain events once a component is added", function () {
var gainListener = sinon.stub();
new pc.EntityReference(testComponent, "myEntity2", {
"dummy#gain": gainListener
});
testComponent.myEntity2 = otherEntity2.getGuid();
expect(gainListener.callCount).to.equal(0);
otherEntity2.addComponent("dummy", {});
expect(gainListener.callCount).to.equal(1);
});
it("fires component lose and gain events when a component is removed and re-added", function () {
var gainListener = sinon.stub();
var loseListener = sinon.stub();
new pc.EntityReference(testComponent, "myEntity1", {
"dummy#gain": gainListener,
"dummy#lose": loseListener
});
testComponent.myEntity1 = otherEntity1.getGuid();
expect(gainListener.callCount).to.equal(1);
expect(loseListener.callCount).to.equal(0);
otherEntity1.removeComponent("dummy");
expect(gainListener.callCount).to.equal(1);
expect(loseListener.callCount).to.equal(1);
otherEntity1.addComponent("dummy", {});
expect(gainListener.callCount).to.equal(2);
expect(loseListener.callCount).to.equal(1);
});
it("fires component lose events when the guid is reassigned, but only for component types that the entity had", function () {
var dummyLoseListener = sinon.stub();
var lightLoseListener = sinon.stub();
new pc.EntityReference(testComponent, "myEntity1", {
"dummy#lose": dummyLoseListener,
"light#lose": lightLoseListener
});
testComponent.myEntity1 = otherEntity1.getGuid();
expect(dummyLoseListener.callCount).to.equal(0);
expect(lightLoseListener.callCount).to.equal(0);
testComponent.myEntity1 = null;
expect(dummyLoseListener.callCount).to.equal(1);
expect(lightLoseListener.callCount).to.equal(0);
});
it("forwards any events dispatched by a component", function () {
var fooListener = sinon.stub();
var barListener = sinon.stub();
new pc.EntityReference(testComponent, "myEntity1", {
"dummy#foo": fooListener,
"dummy#bar": barListener
});
testComponent.myEntity1 = otherEntity1.getGuid();
otherEntity1.dummy.fire("foo", "a", "b");
expect(fooListener.callCount).to.equal(1);
expect(fooListener.getCall(0).args[0]).to.equal("a");
expect(fooListener.getCall(0).args[1]).to.equal("b");
expect(barListener.callCount).to.equal(0);
otherEntity1.dummy.fire("bar", "c", "d");
expect(fooListener.callCount).to.equal(1);
expect(barListener.callCount).to.equal(1);
expect(barListener.getCall(0).args[0]).to.equal("c");
expect(barListener.getCall(0).args[1]).to.equal("d");
});
it("correctly handles component event forwarding across component removal and subsequent re-addition", function () {
var fooListener = sinon.stub();
var barListener = sinon.stub();
new pc.EntityReference(testComponent, "myEntity1", {
"dummy#foo": fooListener,
"dummy#bar": barListener
});
testComponent.myEntity1 = otherEntity1.getGuid();
var oldDummyComponent = otherEntity1.dummy;
otherEntity1.removeComponent("dummy");
oldDummyComponent.fire("foo");
oldDummyComponent.fire("bar");
expect(fooListener.callCount).to.equal(0);
expect(barListener.callCount).to.equal(0);
var newDummyComponent = otherEntity1.addComponent("dummy");
newDummyComponent.fire("foo");
newDummyComponent.fire("bar");
expect(fooListener.callCount).to.equal(1);
expect(barListener.callCount).to.equal(1);
});
it("forwards any events dispatched by the entity", function () {
var fooListener = sinon.stub();
var barListener = sinon.stub();
new pc.EntityReference(testComponent, "myEntity1", {
"entity#foo": fooListener,
"entity#bar": barListener
});
testComponent.myEntity1 = otherEntity1.getGuid();
otherEntity1.fire("foo", "a", "b");
expect(fooListener.callCount).to.equal(1);
expect(fooListener.getCall(0).args[0]).to.equal("a");
expect(fooListener.getCall(0).args[1]).to.equal("b");
expect(barListener.callCount).to.equal(0);
otherEntity1.fire("bar", "c", "d");
expect(fooListener.callCount).to.equal(1);
expect(barListener.callCount).to.equal(1);
expect(barListener.getCall(0).args[0]).to.equal("c");
expect(barListener.getCall(0).args[1]).to.equal("d");
});
it("correctly handles entity event forwarding across entity nullification and subsequent reassignment", function () {
var fooListener = sinon.stub();
var barListener = sinon.stub();
new pc.EntityReference(testComponent, "myEntity1", {
"entity#foo": fooListener,
"entity#bar": barListener
});
testComponent.myEntity1 = otherEntity1.getGuid();
testComponent.myEntity1 = null;
otherEntity1.fire("foo");
otherEntity1.fire("bar");
expect(fooListener.callCount).to.equal(0);
expect(barListener.callCount).to.equal(0);
testComponent.myEntity1 = otherEntity2.getGuid();
otherEntity2.fire("foo");
otherEntity2.fire("bar");
expect(fooListener.callCount).to.equal(1);
expect(barListener.callCount).to.equal(1);
});
it("validates the event map", function () {
function testEventMap(eventMap) {
new pc.EntityReference(testComponent, "myEntity1", eventMap);
}
var callback = sinon.stub();
expect(function() {
testEventMap({ "foo": callback });
}).to.throw("Invalid event listener description: `foo`");
expect(function() {
testEventMap({ "foo#": callback });
}).to.throw("Invalid event listener description: `foo#`");
expect(function() {
testEventMap({ "#foo": callback });
}).to.throw("Invalid event listener description: `#foo`");
expect(function() {
testEventMap({ "foo#bar": null });
}).to.throw("Invalid or missing callback for event listener `foo#bar`");
});
it("logs a warning if the entity property is set to anything other than a string, undefined or null", function () {
sinon.stub(console, "warn");
new pc.EntityReference(testComponent, "myEntity1");
testComponent.myEntity1 = otherEntity1.getGuid();
testComponent.myEntity1 = null;
testComponent.myEntity1 = undefined;
expect(console.warn.callCount).to.equal(0);
testComponent.myEntity1 = {};
expect(console.warn.callCount).to.equal(1);
expect(console.warn.getCall(0).args[0]).to.equal("Entity field `myEntity1` was set to unexpected type 'object'");
});
it("set reference to a pc.Entity instead of guid, converts property to guid", function () {
var reference = new pc.EntityReference(testComponent, "myEntity1");
testComponent.myEntity1 = otherEntity1;
expect(testComponent.myEntity1).to.equal(otherEntity1.getGuid(), "Component property converted to guid");
expect(reference.entity).to.equal(otherEntity1);
});
it("set reference to a pc.Entity that is not in hierarchy, converts property to guid", function () {
var reference = new pc.EntityReference(testComponent, "myEntity1");
var entity = new pc.Entity();
testComponent.myEntity1 = entity;
expect(testComponent.myEntity1).to.equal(entity.getGuid(), "Component property converted to guid");
expect(reference.entity).to.equal(entity);
});
it("hasComponent() returns false if the entity is not present", function () {
var reference = new pc.EntityReference(testComponent, "myEntity1");
expect(reference.hasComponent("dummy")).to.equal(false);
});
it("hasComponent() returns false if the entity is present but does not have a component of the provided type", function () {
var reference = new pc.EntityReference(testComponent, "myEntity1");
testComponent.myEntity1 = otherEntity1.getGuid();
otherEntity1.removeComponent("dummy");
expect(reference.hasComponent("dummy")).to.equal(false);
});
it("hasComponent() returns true if the entity is present and has a component of the provided type", function () {
var reference = new pc.EntityReference(testComponent, "myEntity1");
testComponent.myEntity1 = otherEntity1.getGuid();
expect(reference.hasComponent("dummy")).to.equal(true);
});
});