Home | History | Annotate | Download | only in js
      1 suite('group-player-finish-event', function() {
      2   setup(function() {
      3     document.timeline.currentTime = undefined;
      4     this.element = document.createElement('div');
      5     document.documentElement.appendChild(this.element);
      6     var animation = new AnimationSequence([
      7       new Animation(this.element, [], 500),
      8       new AnimationGroup([
      9         new Animation(this.element, [], 250),
     10         new Animation(this.element, [], 500),
     11       ]),
     12     ]);
     13     this.player = document.timeline.play(animation, 1000);
     14   });
     15   teardown(function() {
     16     if (this.element.parent)
     17       this.element.removeChild(this.element);
     18   });
     19 
     20   test('fire when player completes', function(done) {
     21     var ready = false;
     22     var fired = false;
     23     var player = this.player;
     24     player.onfinish = function(event) {
     25       assert(ready, 'must not be called synchronously');
     26       assert.equal(this, player);
     27       assert.equal(event.target, player);
     28       assert.equal(event.currentTime, 1000);
     29       assert.equal(event.timelineTime, 1100);
     30       if (fired)
     31         assert(false, 'must not get fired twice');
     32       fired = true;
     33       done();
     34     };
     35     tick(100);
     36     tick(1100);
     37     tick(2100);
     38     ready = true;
     39   });
     40 
     41   test('fire when reversed player completes', function(done) {
     42     this.player.onfinish = function(event) {
     43       assert.equal(event.currentTime, 0);
     44       assert.equal(event.timelineTime, 1001);
     45       done();
     46     };
     47     tick(0);
     48     tick(500);
     49     this.player.reverse();
     50     tick(501);
     51     tick(1001);
     52   });
     53 
     54   test('fire after player is cancelled', function(done) {
     55     this.player.onfinish = function(event) {
     56       assert.equal(event.currentTime, 0);
     57       assert.equal(event.timelineTime, 1, 'event must be fired on next sample');
     58       done();
     59     };
     60     tick(0);
     61     this.player.cancel();
     62     tick(1);
     63   });
     64 
     65   test('multiple event listeners', function(done) {
     66     var count = 0;
     67     function createHandler(expectedCount) {
     68       return function() {
     69         count++;
     70         assert.equal(count, expectedCount);
     71       };
     72     }
     73     var toRemove = createHandler(0);
     74     this.player.addEventListener('finish', createHandler(1));
     75     this.player.addEventListener('finish', createHandler(2));
     76     this.player.addEventListener('finish', toRemove);
     77     this.player.addEventListener('finish', createHandler(3));
     78     this.player.removeEventListener('finish', toRemove);
     79     this.player.onfinish = function() {
     80       assert.equal(count, 3);
     81       done();
     82     };
     83     tick(0);
     84     this.player.cancel();
     85     tick(1000);
     86   });
     87 });
     88