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