-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.js
576 lines (432 loc) · 15.6 KB
/
test.js
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
var test = require('tape'),
assert = require('assert'),
animitter = require('./index');
function invoke(arr, fnString){
arr.forEach(function(item){
item[fnString]();
});
}
//create a fn that completes that loop at frame 100
function completeAtFrame(done, frame){
return function(deltaTime, elapsedTime, frameCount){
if( frameCount === frame ){
this.complete();
}
};
}
function createLoops( n ){
var threads = [];
var noop = function(){};
for( var i=0; i<n; i++){
threads.push(animitter(noop));
}
return threads;
}
function okDelta(fps, deltaTime){
var fpsMS = ~~(1000/fps);
return (deltaTime === 0 || deltaTime>(fpsMS-10)) && deltaTime - fpsMS < fpsMS;
}
test('running counter', function(t){
t.plan(5);
var n = 100;
var loops;
//havent been started yet
loops = createLoops(n);
t.equal(animitter.running, 0, 'should have 0 animations running after creation');
//started now
invoke(loops, 'start');
t.equal(animitter.running, n, 'all ' + n + ' loops running');
//stopped
invoke(loops, 'stop');
t.equal(animitter.running, 0, 'all ' + n + ' loops should be stopped and not counted as running');
invoke(loops, 'start');
//half are stopped half are completed
loops.forEach(function(loop, i){
loop[ (i%2===0) ? 'stop' : 'complete' ]();
});
t.equal(animitter.running, 0, 'complete() and stop() should both drop the running counter');
//repeatedly stopped, counter should not go negative
invoke(loops, 'start');
invoke(loops, 'stop');
invoke(loops, 'complete');
invoke(loops, 'stop');
t.equal(animitter.running, 0, 'should not go negative when calling stop multiple times in a row');
});
test('load animitter', function(t){
t.plan(2);
//load it
t.equal(typeof animitter, 'function', 'animitter module should be a function');
t.equal(typeof animitter.Animitter.prototype.emit, 'function', 'emit should be a function on the prototype object');
});
test('invoke animitter()', function(t){
t.plan(3);
//create a loop but not start it
var loop = animitter();
t.equal(loop.isRunning(), false, 'should not be animating since the loop has not started');
t.equal(loop.frameCount, 0, 'should not have updated yet');
loop.start();
t.equal(loop.isRunning(), true, 'should be animating now that start() has been called');
loop.stop();
//loop.dispose();
});
test('animitter().dispose()', function(t){
var events = ['start', 'update', 'stop', 'complete'];
t.plan(1 + events.length);
var cbUpdate = function(){};
var cbUpdate2 = function(){};
var cbStart = function(){};
var cbStop = function(){};
var cbComplete = function(){};
var loop = animitter()
.on('start', cbStart)
.on('update', cbUpdate)
.on('update', cbUpdate2)
.on('stop', cbStop)
.on('complete', cbComplete);
loop.start();
setTimeout(function(){
//should stop the loop and remove all listeners
loop.dispose();
t.ok(!loop.isRunning(), 'should not be animating');
events.forEach(function(evtName){
t.equal(loop.listeners(evtName).length, 0, 'should have all listeners removed for ' + evtName);
});
}, 33);
});
test('animitter().update()', function(t){
t.plan(2);
//calling update() without calling start()
var loop = animitter(function(deltaTime, elapsedTime, frameCount){
t.equal(loop.isRunning(), false, 'false cause it was never started');
t.equal(frameCount, 1, 'should run only once');
});
loop.update();
});
test('animitter({ fps: fps })', function(t){
t.plan(1);
var fps = 15;
var loop = animitter({ fps: fps });
t.equal(loop.getFPSLimit(), fps);
});
test('callback(deltaTime, elapsedTime, frameCount)', function(t){
var totalFrames = 10;
t.plan(4);
var loop = animitter();
var frame = 0;
var dt = true,
et = true,
fc = true,
fc2 = true;
loop.on('update', function(deltaTime, elapsedTime, frameCount){
frame++;
dt && (dt = deltaTime === this.getDeltaTime());
et && (et = elapsedTime === this.getElapsedTime());
fc && (fc = frameCount === this.getFrameCount());
fc2 && (fc2 = frameCount === frame);
if(frameCount === totalFrames){
this.complete();
}
});
loop.on('complete', function(){
t.ok(dt, 'deltaTime matches getDeltaTime()');
t.ok(et, 'elapsedTime matches getElapsedTime()');
t.ok(fc, 'frameCount matches getFrameCount()');
t.ok(fc2, 'frameCount matches counter');
});
loop.start();
});
test('animitter().getFrameCount()', function(t){
var totalFrames = 10;
t.plan(3);
var fcOk = true;
animitter()
.on('update', function(delta, elapsed, frameCount){
fcOk && (fcOk = frameCount === this.getFrameCount());
if(frameCount === totalFrames){
this.complete();
}
})
.on('complete', function(delta, elapsed, frameCount){
t.ok(fcOk, 'frameCount should be consistent');
t.equal(this.getFrameCount(), totalFrames, 'frameCount should be ' + totalFrames + ' when complete');
this.reset();
})
.on('reset', function(delta, elapsed, frameCount){
t.equal(this.getFrameCount(), 0, 'frameCount should be 0 when reset');
})
.start();
});
test('animitter().getElapsedTime() without being started', function(t){
t.plan(1);
var loop = animitter();
setTimeout(function(){
t.equal(loop.getElapsedTime(), 0, 'should be 0');
});
});
test('animitter().getElapsedTime() after reset', function(t){
t.plan(1);
var loop = animitter(function(delta, elapsed, frame){
if(frame === 10){
this.reset();
}
});
loop.on('reset', function(delta, elapsed, frame){
t.equal(loop.getElapsedTime(), 0, 'should be 0');
});
loop.start();
});
test('animitter().getElapsedTime() should track total time played', function(t){
t.plan(1);
var delay = 1000;
var thresh = 32;
var loop = animitter();
loop.on('start', function(){
setTimeout(function(){
t.ok(loop.getElapsedTime() > delay-thresh && loop.getElapsedTime() < delay + thresh, 'should accurately track time');
loop.complete();
}, delay);
});
loop.start();
});
test('animitter().getElapsedTime() should not count time while stopped', function(t){
t.plan(2);
var loop = animitter.bound();
loop.on('update', function waitToPause(dt, et){
if(et > 120){
loop.stop();
loop.off('update', waitToPause);
loop.once('update', function(deltaTime, elapsedTime){
t.ok(deltaTime < 100);
t.ok(elapsedTime < 300);
loop.stop();
});
setTimeout(loop.start, 200);
}
});
loop.start();
});
test('animitter().getElapsedTime() should stop counting forward when stopped', function(t){
t.plan(1);
var lastElapsed;
var loop = animitter();
loop.on('start', function(){
var loop = this,
delay = 1000;
setTimeout(function(){
t.equal(loop.getElapsedTime(), lastElapsed, 'elapsed time stopped counting forward after stopped');
}, delay);
});
loop.once('update', function(deltaTime, elapsedTime){
this.stop();
lastElapsed = elapsedTime;
});
loop.start();
});
test('animitter().complete() should trigger complete', function(t){
t.plan(1);
animitter(completeAtFrame(null,30))
.on('complete', function(delta, elapsed, frameCount){
t.ok(this.isCompleted(), 'complete triggered');
})
.start();
});
test('animitter().reset() should reset a loop for reuse', function(t){
t.plan(2);
var lastFrame = 100;
var timesCompleted = 0;
var timesReset = 0;
var loop = animitter(completeAtFrame(null,lastFrame));
//on first complete, reset, on 2nd complete, done()
loop.on('complete', function(delta, elapsed, frameCount){
timesCompleted++;
if(timesCompleted === 1){
loop.reset();
} else {
t.equal(timesReset, 1, 'loop has been reset twice');
}
});
//on first reset, start again
loop.on('reset', function(delta, elapsed, frameCount){
//expect frameCount to be 0, cause it hasnt started back up yet
t.equal(frameCount, 0, 'frameCount should be reset');
timesReset++;
loop.start();
});
loop.start();
});
test('should trigger stop event and stop the loop', function(t){
t.plan(3);
var totalFrames = 10;
var loop = animitter()
.on('update', function(delta, elapsed, frameCount){
if(frameCount === totalFrames){
this.stop();
}
})
.on('stop', function(delta, elapsed, frameCount){
t.equal(frameCount, totalFrames, 'stopped at ' + totalFrames);
t.notOk(this.isRunning(), 'is not animating');
t.notOk(this.isCompleted(), 'is not completed');
})
.start();
});
test('animitter({ fps: fps }, fn)', function(t){
//we aren't testing 60 because that is the
//responsibility of requestAnimationFrame to throttle correctly
var frameRateRuns = [45, 30, 15, 5];
var totalFrames = 10;
//an assertion every update, and one for complete for each run
t.plan(frameRateRuns.length * totalFrames + frameRateRuns.length);
function testAt(fps){
var fpsMS = ~~(1000/fps);
var loop = animitter({ fps: fps }, function(deltaTime, elapsedTime, frameCount){
//first frame will be fast
//deltaTime would be 0 on the first frame
//fpsMS-9 is the desired time minus half of a 60fps frame (8.5ms)
t.ok(okDelta(fps, deltaTime), 'paced at ' + fps + 'fps, deltaTime = ' + deltaTime);
if(frameCount === totalFrames){
this.complete();
}
});
loop.on('complete', function(){
t.equal(loop.isCompleted(), true, 'should be completed');
})
return loop;
};
//this test is sensitive to other tests, so we
//will wait 5 seconds before beginning to start
setTimeout(function(){
invoke(frameRateRuns.map(testAt), 'start');
}, 3000);
});
test('animitter().setFPS(fps)', function(t){
var frameRates = [30, 10, 45, 20, 5];
t.plan(frameRates.length);
var fps = frameRates.shift();
var loop = animitter({ fps: fps });
loop.update();
loop.on('update', function(delta){
t.ok(okDelta(fps, delta), 'pace set at ' + fps + 'fps' +', deltaTime = ' + delta);
if( frameRates.length ){
fps = frameRates.shift();
loop.setFPS(fps);
} else {
this.complete();
}
});
loop.start();
});
test('animitter().getFPS() should return the calculated framerate from the last deltaTime', function(t){
var fps = 30;
t.plan(4);
var loop = animitter({ fps: fps }, function(deltaTime, elapsedTime, frameCount){
var currentFPS = loop.getFPS();
console.log('currentFPS = ' + currentFPS)
t.equals(currentFPS, 1000 / deltaTime, 'should be the fps as calculated by the last deltaTime');
if(frameCount === 2){
this.stop();
//make sure it doesnt change just sitting here waiting
setTimeout(function(){
t.equals(loop.getFPS(), currentFPS, 'should not change while stopped');
loop.dispose();
}, 120);
}
});
t.equal(loop.getFPS(), 0, 'should equal 0 as the loop hasnt started yet');
loop.start();
});
test('animitter().getFPSLimit() should return what was set in setFPS(fps)', function(t){
t.plan(3);
var loop = animitter();
t.equals(loop.getFPSLimit(), Infinity, 'default value should be Inifnity');
var fps = 30;
loop.setFPS(fps);
t.equals(loop.getFPSLimit(), fps, 'should equal set value');
t.equals(animitter({ fps: fps }).getFPSLimit(), fps, 'should equal value from options object');
});
test('animitter.bound()', function(t){
t.plan(1);
var loop = animitter.bound();
loop.on('update', function(){
t.ok(this instanceof animitter.Animitter);
});
setTimeout(loop.update, 10);
});
test('animitter({ fixedDelta: true })', function(t){
t.plan(6);
var loop = animitter({ fixedDelta: true});
testFixedDelta(loop, t, function(){
testNonFixedDelta(loop, t);
});
});
test('animitter.globalFixedDelta : Boolean', function(t){
animitter.globalFixedDelta = true;
t.plan(12);
testFixedDelta(animitter(), t, function(){
testFixedDelta(animitter({ fps: 30 }), t, function(){
animitter.globalFixedDelta = false;
testNonFixedDelta(animitter(), t);
testNonFixedDelta(animitter({ fps: 30 }), t);
});
});
});
function testFixedDelta(loop, t, callback){
callback = callback || function(){};
loop.once('update', function(delta, elapsed, frameCount){
t.equal(delta, 1000/Math.min(60, Math.min(60, this.getFPSLimit())), 'delta should be fixed');
t.equal(elapsed, 1000/Math.min(60, Math.min(60, this.getFPSLimit())) * frameCount);
});
loop.update();
setTimeout(function(){
loop.once('update', function(delta, elapsed, frameCount){
t.equal(delta, 1000/Math.min(60, this.getFPSLimit()));
t.equal(elapsed, 1000/Math.min(60, this.getFPSLimit()) * frameCount);
});
loop.update();
callback();
}, 200);
}
function testNonFixedDelta(loop, t, callback){
callback = callback || function(){};
//toggle it at any time
loop.fixedDelta = false;
loop.update();
setTimeout(function(){
loop.once('update', function(delta, elapsed, frameCount){
t.ok(delta > 1000/Math.min(60, this.getFPSLimit()) * frameCount, '3rd frame delta should not be fixed');
t.ok(elapsed > 1000/Math.min(60, this.getFPSLimit()) * frameCount);
callback();
});
loop.update();
}, 300);
}
test('animitter().setRequestAnimationFrameObject(object)', function(t){
t.plan(7);
function missingParams(){
//no cancelAnimationFrame
animitter().setRequestAnimationFrameObject({});
}
t.ok(missingParams, /Invalid object/, 'should throw an error of invalid parameters');
var loop = animitter();
var original = loop.getRequestAnimationFrameObject();
var mock = {
requestAnimationFrame: request,
cancelAnimationFrame: cancel
};
function request(){}
function cancel(){}
loop.setRequestAnimationFrameObject(mock);
var animationFrame = loop.getRequestAnimationFrameObject();
t.equal(animationFrame.requestAnimationFrame, request);
t.equal(animationFrame.cancelAnimationFrame, cancel);
loop.setRequestAnimationFrameObject(original);
animationFrame = loop.getRequestAnimationFrameObject();
t.equal(animationFrame.requestAnimationFrame, original.requestAnimationFrame);
t.equal(animationFrame.cancelAnimationFrame, original.cancelAnimationFrame);
//test that a newly created loop receives the default requestAnimationFrame
loop.setRequestAnimationFrameObject(mock);
var loop2 = animitter();
t.notEqual(loop.getRequestAnimationFrameObject(), loop2.getRequestAnimationFrameObject());
//test that this can be set from the options argument
t.equal(animitter({ requestAnimationFrameObject: mock }).getRequestAnimationFrameObject(), mock);
});