aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/fx/animation_test.html
blob: 30d7243bbc3c03b9347db32b92e7d78ed6fd4dde (plain)
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
<!DOCTYPE html>
<html>
<!--
Copyright 2011 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.fx.Animation</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.events');
  goog.require('goog.fx.Animation');
  goog.require('goog.testing.MockClock');
  goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>
  var clock;

  function setUpPage() {
    clock = new goog.testing.MockClock(true);
  }

  function tearDownPage() {
    clock.dispose();
  }

  function testPauseLogic() {
    var anim = new goog.fx.Animation([], [], 3000);
    var nFrames = 0;
    goog.events.listen(anim, goog.fx.Animation.EventType.ANIMATE, function(e) {
      assertRoughlyEquals(e.progress, progress, 1e-6);
      nFrames++;
    });
    goog.events.listen(anim, goog.fx.Animation.EventType.END, function(e) {
      nFrames++;
    });
    var nSteps = 10;
    for (var i = 0; i < nSteps; i++) {
      progress = i / (nSteps - 1);
      anim.setProgress(progress);
      anim.play();
      anim.pause();
    }
    assertEquals(nSteps, nFrames);
  }

  function testPauseOffset() {
    var anim = new goog.fx.Animation([0], [1000], 1000);
    anim.play();

    assertEquals(0, anim.coords[0]);
    assertRoughlyEquals(0, anim.progress, 1e-4);

    clock.tick(300);

    assertEquals(300, anim.coords[0]);
    assertRoughlyEquals(0.3, anim.progress, 1e-4);

    anim.pause();

    clock.tick(400);

    assertEquals(300, anim.coords[0]);
    assertRoughlyEquals(0.3, anim.progress, 1e-4);

    anim.play();

    assertEquals(300, anim.coords[0]);
    assertRoughlyEquals(0.3, anim.progress, 1e-4);

    clock.tick(400);

    assertEquals(700, anim.coords[0]);
    assertRoughlyEquals(0.7, anim.progress, 1e-4);

    anim.pause();

    clock.tick(300);

    assertEquals(700, anim.coords[0]);
    assertRoughlyEquals(0.7, anim.progress, 1e-4);

    anim.play();

    var lastPlay = goog.now();

    assertEquals(700, anim.coords[0]);
    assertRoughlyEquals(0.7, anim.progress, 1e-4);

    clock.tick(300);

    assertEquals(1000, anim.coords[0]);
    assertRoughlyEquals(1, anim.progress, 1e-4);
    assertEquals(goog.fx.Animation.State.STOPPED, anim.getStateInternal());
  }

  function testSetProgress() {
    var anim = new goog.fx.Animation([0], [1000], 3000);
    var nFrames = 0;
    anim.play();
    anim.setProgress(0.5);
    goog.events.listen(anim, goog.fx.Animation.EventType.ANIMATE, function(e) {
      assertEquals(500, e.coords[0]);
      assertRoughlyEquals(0.5, e.progress, 1e-4);
      nFrames++;
    });
    anim.cycle(goog.now());
    anim.stop();
    assertEquals(1, nFrames);
  }
</script>
</body>