Home | History | Annotate | Download | only in js
      1 suite('animation-node', function() {
      2   test('normalize timing input', function() {
      3     assert.equal(normalizeTimingInput(1).duration, 1);
      4     assert.equal(normalizeTimingInput(1).easing(0.2), 0.2);
      5     assert.equal(normalizeTimingInput(undefined).duration, 0);
      6   });
      7   test('calculating active duration', function() {
      8     assert.equal(calculateActiveDuration({duration: 1000, playbackRate: 4, iterations: 20}), 5000);
      9     assert.equal(calculateActiveDuration({duration: 500, playbackRate: 0.1, iterations: 300}), 1500000);
     10   });
     11   test('conversion of timing functions', function() {
     12     var f = toTimingFunction('ease');
     13     var g = toTimingFunction('cubic-bezier(.25, 0.1, 0.25, 1.)');
     14     for (var i = 0; i < 1; i += 0.1) {
     15       assert.equal(f(i), g(i));
     16     }
     17     assert.closeTo(f(0.1844), 0.2601, 0.01);
     18     assert.closeTo(g(0.1844), 0.2601, 0.01);
     19 
     20     f = toTimingFunction('cubic-bezier(0, 1, 1, 0)');
     21     assert.closeTo(f(0.104), 0.392, 0.01);
     22 
     23     function isLinear(f) {
     24       assert.equal(f(0.1), 0.1);
     25       assert.equal(f(0.4), 0.4);
     26       assert.equal(f(0.9), 0.9);
     27     }
     28 
     29     f = toTimingFunction('cubic-bezier(0, 1, -1, 1)');
     30     isLinear(f);
     31 
     32     f = toTimingFunction('an elephant');
     33     isLinear(f);
     34 
     35     f = toTimingFunction('cubic-bezier(-1, 1, 1, 1)');
     36     isLinear(f);
     37 
     38     f = toTimingFunction('cubic-bezier(1, 1, 1)');
     39     isLinear(f);
     40 
     41     f = toTimingFunction('steps(10, end)');
     42     assert.equal(f(0), 0);
     43     assert.equal(f(0.09), 0);
     44     assert.equal(f(0.1), 0.1);
     45     assert.equal(f(0.25), 0.2);
     46   });
     47   test('calculating phase', function() {
     48     // calculatePhase(activeDuration, localTime, timing);
     49     assert.equal(calculatePhase(1000, 100, {delay: 0}), PhaseActive);
     50     assert.equal(calculatePhase(1000, 100, {delay: 200}), PhaseBefore);
     51     assert.equal(calculatePhase(1000, 2000, {delay: 200}), PhaseAfter);
     52     assert.equal(calculatePhase(1000, null, {delay: 200}), PhaseNone);
     53   });
     54   test('calculating active time', function() {
     55     // calculateActiveTime(activeDuration, fillMode, localTime, phase, delay);
     56     assert.equal(calculateActiveTime(1000, 'forwards', 100, PhaseActive, 0), 100);
     57     assert.equal(calculateActiveTime(1000, 'forwards', 100, PhaseBefore, 200), null);
     58     assert.equal(calculateActiveTime(1000, 'both', 100, PhaseBefore, 200), 0);
     59     assert.equal(calculateActiveTime(1000, 'forwards', 500, PhaseActive, 200), 300);
     60     assert.equal(calculateActiveTime(1000, 'forwards', 1100, PhaseAfter, 200), 1000);
     61     assert.equal(calculateActiveTime(1000, 'none', 1100, PhaseAfter, 200), null);
     62     assert.equal(calculateActiveTime(Infinity, 'both', 5000000, PhaseActive, 2000000), 3000000);
     63     assert.equal(calculateActiveTime(Infinity, 'both', 50000, PhaseBefore, 2000000), 0);
     64   });
     65   test('calculating scaled active time', function() {
     66     // calculateScaledActiveTime(activeDuration, activeTime, startOffset, timingInput);
     67     assert.equal(calculateScaledActiveTime(1000, 200, 300, {playbackRate: 1.5}), 600);
     68     assert.equal(calculateScaledActiveTime(1000, 200, 300, {playbackRate: -4}), 3500);
     69     assert.equal(calculateScaledActiveTime(Infinity, 400, 200, {playbackRate: 1}), 600);
     70     assert.equal(calculateScaledActiveTime(Infinity, 400, 200, {playbackRate: -4}), Infinity);
     71   });
     72   test('calculating iteration time', function() {
     73     // calculateIterationTime(iterationDuration, repeatedDuration, scaledActiveTime, startOffset, timingInput);
     74     assert.equal(calculateIterationTime(500, 5000, 600, 100, {iterations: 10, iterationStart: 0}), 100);
     75     assert.equal(calculateIterationTime(500, 5000, Infinity, 100, {iterations: 10, iterationStart: 0}), 500);
     76     assert.equal(calculateIterationTime(500, 5000, 5100, 100, {iterations: 3.2, iterationStart: 0.8}), 500);
     77   });
     78   test('calculating current iteration', function() {
     79     // calculateCurrentIteration(iterationDuration, iterationTime, scaledActiveTime, timingInput);
     80     assert.equal(calculateCurrentIteration(1000, 400, 4400, {iterations: 50, iterationStart: 0.8}), 4);
     81     assert.equal(calculateCurrentIteration(1000, 1000, 4400, {iterations: 50.2, iterationStart: 0.8}), 50);
     82   });
     83   test('calculating transformed time', function() {
     84     // calculateTransformedTime(currentIteration, iterationDuration, iterationTime, timingInput);
     85     assert.equal(calculateTransformedTime(4, 1000, 200, {easing: function(x) { return x; }, direction: 'normal'}), 200);
     86     assert.equal(calculateTransformedTime(4, 1000, 200, {easing: function(x) { return x; }, direction: 'reverse'}), 800);
     87     assert.closeTo(calculateTransformedTime(4, 1000, 200, {easing: function(x) { return x * x; }, direction: 'reverse'}), 640, 0.0001);
     88     assert.closeTo(calculateTransformedTime(4, 1000, 600, {easing: function(x) { return x * x; }, direction: 'alternate'}), 360, 0.0001);
     89     assert.closeTo(calculateTransformedTime(3, 1000, 600, {easing: function(x) { return x * x; }, direction: 'alternate'}), 160, 0.0001);
     90     assert.closeTo(calculateTransformedTime(4, 1000, 600, {easing: function(x) { return x * x; }, direction: 'alternate-reverse'}), 160, 0.0001);
     91     assert.closeTo(calculateTransformedTime(3, 1000, 600, {easing: function(x) { return x * x; }, direction: 'alternate-reverse'}), 360, 0.0001);
     92   });
     93   test('Animation Node', function() {
     94     var timing = normalizeTimingInput({duration: 1000, iterations: 4, iterationStart: 0.5, easing: 'linear', direction: 'alternate', delay: 100, fill: 'forwards'});
     95     var timing2 = normalizeTimingInput({duration: 1000, iterations: 4, iterationStart: 0.5, easing: 'ease', direction: 'alternate', delay: 100, fill: 'forwards'});
     96     var node = webAnimations1.AnimationNode(timing);
     97     var node2 = webAnimations1.AnimationNode(timing2);
     98     assert.equal(node(0), null);
     99     assert.equal(node(100), 0.5);
    100     assert.closeTo(node2(100), 0.8, 0.005);
    101     assert.equal(node(600), 1);
    102     assert.closeTo(node2(600), 1, 0.005);
    103     assert.equal(node(700), 0.9);
    104     assert.closeTo(node2(700), 0.99, 0.005);
    105     assert.equal(node(1600), 0);
    106     assert.closeTo(node2(1600), 0, 0.005);
    107     assert.equal(node(4000), 0.4);
    108     assert.closeTo(node2(4000), 0.68, 0.005);
    109     assert.equal(node(4100), 0.5);
    110     assert.closeTo(node2(4100), 0.8, 0.005);
    111     assert.equal(node(6000), 0.5);
    112     assert.closeTo(node2(6000), 0.8, 0.005);
    113   });
    114 });
    115