2015-08-04 18:32:36 +08:00
|
|
|
var torque = require('../lib/torque');
|
|
|
|
var sinon = require('sinon');
|
|
|
|
require('phantomjs-polyfill');
|
2015-05-04 17:05:48 +08:00
|
|
|
|
2015-08-05 20:40:52 +08:00
|
|
|
QUnit.module('animator');
|
|
|
|
|
2015-08-05 18:49:02 +08:00
|
|
|
test('time moves', function(assert) {
|
|
|
|
var done = assert.async();
|
2015-08-05 20:36:59 +08:00
|
|
|
var animatora = new torque.Animator(function(){}, {steps: 500, animationDuration: 2});
|
|
|
|
animatora.start();
|
2015-08-05 18:34:55 +08:00
|
|
|
setTimeout(function(){
|
2015-08-05 20:36:59 +08:00
|
|
|
assert.notEqual(animatora._time, 0);
|
2017-11-07 22:38:42 +08:00
|
|
|
animatora.pause();
|
2015-08-05 18:49:02 +08:00
|
|
|
done();
|
2015-08-05 20:27:29 +08:00
|
|
|
}, 20)
|
2015-08-05 18:34:55 +08:00
|
|
|
});
|
2015-05-04 17:05:48 +08:00
|
|
|
|
2015-08-04 18:32:36 +08:00
|
|
|
test("rescale should resume animation if previously playing", function(assert){
|
2015-08-05 20:27:29 +08:00
|
|
|
var animator = new torque.Animator(function(){}, {steps: 500, animationDuration: 2});
|
2015-08-04 18:32:36 +08:00
|
|
|
animator.toggle();
|
|
|
|
animator.rescale();
|
|
|
|
assert.ok(animator.running);
|
|
|
|
animator.pause()
|
|
|
|
});
|
|
|
|
|
2015-08-04 22:30:02 +08:00
|
|
|
test("rescale shouldn't resume animation if previously paused", function(assert){
|
2015-08-05 20:27:29 +08:00
|
|
|
var animator = new torque.Animator(function(){}, {steps: 500, animationDuration: 2});
|
2015-08-04 22:30:02 +08:00
|
|
|
animator.pause();
|
|
|
|
animator.rescale();
|
|
|
|
assert.notOk(animator.running);
|
|
|
|
});
|
|
|
|
|
2015-08-05 18:34:55 +08:00
|
|
|
test("onStart runs properly", function(assert){
|
2015-08-05 20:27:29 +08:00
|
|
|
var animator = new torque.Animator(function(){}, {steps: 500, animationDuration: 2});
|
2015-08-05 18:34:55 +08:00
|
|
|
animator.options.onStop = function(){
|
|
|
|
assert.ok(true);
|
|
|
|
animator.pause();
|
|
|
|
};
|
|
|
|
animator.stop();
|
|
|
|
});
|
2015-08-04 18:32:36 +08:00
|
|
|
|
2015-11-25 01:34:46 +08:00
|
|
|
test(".stepsRange sets a custom range with valid input", function(assert){
|
|
|
|
var animator = new torque.Animator(function(){}, {steps: 500, animationDuration: 2});
|
|
|
|
var customStepsRange = animator.stepsRange(101, 202);
|
|
|
|
assert.ok(customStepsRange);
|
|
|
|
assert.equal(customStepsRange.start, 101);
|
|
|
|
assert.equal(customStepsRange.end, 202);
|
|
|
|
|
|
|
|
var didCallOnStepsRange = false;
|
|
|
|
var animator = new torque.Animator(function(){}, {steps: 500, animationDuration: 2,
|
|
|
|
onStepsRange: function() {
|
|
|
|
didCallOnStepsRange = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
animator.stepsRange(101, 202);
|
|
|
|
assert.ok(didCallOnStepsRange);
|
|
|
|
});
|
|
|
|
|
|
|
|
test(".stepsRange throws error if given range is outside default range", function(assert){
|
|
|
|
var animator = new torque.Animator(function(){}, {steps: 500, animationDuration: 2});
|
|
|
|
assert.throws(function() { animator.stepsRange(1, 501) });
|
|
|
|
assert.throws(function() { animator.stepsRange(-1, 500) });
|
|
|
|
assert.throws(function() { animator.stepsRange(-1, 9000) });
|
|
|
|
});
|
|
|
|
|
|
|
|
test(".removeCustomStepsRange should remove any custom steps range", function(assert){
|
|
|
|
var animator = new torque.Animator(function(){}, {steps: 500, animationDuration: 2});
|
|
|
|
animator.removeCustomStepsRange();
|
|
|
|
animator.stepsRange(101, 202);
|
|
|
|
var customStepsRange = animator.stepsRange();
|
|
|
|
animator.removeCustomStepsRange();
|
|
|
|
var defaultStepsRange = animator.stepsRange();
|
|
|
|
assert.ok(defaultStepsRange);
|
|
|
|
assert.notEqual(defaultStepsRange, customStepsRange);
|
|
|
|
assert.equal(defaultStepsRange.start, 0);
|
|
|
|
assert.equal(defaultStepsRange.end, 500);
|
|
|
|
|
|
|
|
var didCallOnStepsRange = false;
|
|
|
|
var animator = new torque.Animator(function(){}, {steps: 500, animationDuration: 2,
|
|
|
|
onStepsRange: function() {
|
|
|
|
didCallOnStepsRange = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
animator.stepsRange(101, 202);
|
|
|
|
animator.removeCustomStepsRange();
|
|
|
|
assert.ok(didCallOnStepsRange);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("stop should take the pointer to position zero by default", function(assert){
|
2015-08-05 20:27:29 +08:00
|
|
|
var animator = new torque.Animator(function(){}, {steps: 500, animationDuration: 2});
|
2015-08-04 18:32:36 +08:00
|
|
|
animator.stop()
|
|
|
|
assert.equal(animator._time, 0);
|
|
|
|
});
|
|
|
|
|
2015-11-25 01:34:46 +08:00
|
|
|
test("stop should take the pointer to start position for custom range", function(assert){
|
|
|
|
var animator = new torque.Animator(function(){}, {steps: 500, animationDuration: 2});
|
|
|
|
animator.stepsRange(42, 137);
|
|
|
|
animator.stop()
|
|
|
|
assert.equal(animator._time, 42);
|
|
|
|
});
|
|
|
|
|
2015-08-04 18:32:36 +08:00
|
|
|
test("stop should call onStop", function(assert){
|
2015-08-05 20:27:29 +08:00
|
|
|
var animator = new torque.Animator(function(){}, {steps: 500, animationDuration: 2});
|
2015-08-04 18:32:36 +08:00
|
|
|
animator.options.onStop = function(){
|
|
|
|
assert.ok(true);
|
|
|
|
animator.pause();
|
|
|
|
};
|
|
|
|
animator.stop();
|
|
|
|
});
|
|
|
|
|
2015-08-04 21:45:11 +08:00
|
|
|
test("altering steps should rescale", function(assert){
|
2015-08-05 20:27:29 +08:00
|
|
|
var animator = new torque.Animator(function(){}, {steps: 500, animationDuration: 2});
|
2015-08-04 21:45:11 +08:00
|
|
|
sinon.spy(animator, "rescale");
|
|
|
|
animator.steps(600);
|
|
|
|
assert.ok(animator.rescale.calledOnce);
|
|
|
|
});
|
2015-08-04 18:32:36 +08:00
|
|
|
|
2015-11-25 01:34:46 +08:00
|
|
|
test("tick should set time to zero if steps are bigger than default range", function(assert){
|
|
|
|
var done = assert.async();
|
|
|
|
var animatorb = new torque.Animator(function(){}, {steps: 500, animationDuration: 2});
|
|
|
|
animatorb.start();
|
|
|
|
animatorb.step(800);
|
|
|
|
setTimeout(function(){
|
|
|
|
console.log(animatorb.step());
|
|
|
|
assert.ok(animatorb.step() | 0 === 0);
|
|
|
|
done();
|
|
|
|
}, 20);
|
|
|
|
animatorb.pause();
|
|
|
|
});
|
|
|
|
|
|
|
|
test("tick should set time to start step if steps are bigger than custom range", function(assert){
|
2015-08-05 18:49:02 +08:00
|
|
|
var done = assert.async();
|
2015-08-05 20:36:59 +08:00
|
|
|
var animatorb = new torque.Animator(function(){}, {steps: 500, animationDuration: 2});
|
|
|
|
animatorb.start();
|
|
|
|
animatorb.step(800);
|
2015-11-25 01:34:46 +08:00
|
|
|
animatorb.stepsRange(42, 137);
|
2015-08-05 18:35:06 +08:00
|
|
|
setTimeout(function(){
|
2015-08-05 20:36:59 +08:00
|
|
|
console.log(animatorb.step());
|
2015-11-25 01:34:46 +08:00
|
|
|
// round step to an integer
|
|
|
|
assert.ok(animatorb.step() | 0 === 42);
|
2015-08-05 18:49:02 +08:00
|
|
|
done();
|
2015-08-05 20:27:29 +08:00
|
|
|
}, 20);
|
2015-08-05 20:36:59 +08:00
|
|
|
animatorb.pause();
|
2015-08-05 18:35:06 +08:00
|
|
|
});
|
2015-08-04 18:32:36 +08:00
|
|
|
|
2015-08-05 18:49:02 +08:00
|
|
|
test("tick should pause animation on end if loop is disabled", function(assert){
|
2015-08-05 20:27:29 +08:00
|
|
|
var animator = new torque.Animator(function(){}, {steps: 500, animationDuration: 2});
|
2015-08-05 18:35:06 +08:00
|
|
|
animator.options.loop = false;
|
|
|
|
animator.toggle();
|
|
|
|
animator.step(600);
|
2015-08-05 18:55:34 +08:00
|
|
|
assert.equal(animator._time,animator.options.animationDuration);
|
2015-08-05 18:35:06 +08:00
|
|
|
});
|