aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/ui/animatedzippy_test.html
blob: 02a9cd2c43bbd3d7162fb3ee36b7b1a842b2b0b0 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!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.ui.AnimatedZippy</title>
  <script src="../base.js"></script>
  <script>
    goog.require('goog.dom');
    goog.require('goog.dom.a11y');
    goog.require('goog.dom.a11y.State');
    goog.require('goog.events');
    goog.require('goog.functions');
    goog.require('goog.fx.Animation');
    goog.require('goog.fx.Transition.EventType');
    goog.require('goog.testing.PropertyReplacer');
    goog.require('goog.testing.asserts');
    goog.require('goog.testing.jsunit');
    goog.require('goog.ui.AnimatedZippy');
    goog.require('goog.ui.Zippy.Events');
  </script>
  <style type="text/css">

    .demo {
      border: solid 1px red;
      margin: 0 0 20px 0;
    }

    .demo h2 {
      background-color: yellow;
      border: solid 1px #ccc;
      padding: 2px;
      margin: 0;
      fint-size: 100%;
    }

    .demo div {
      border: solid 1px #ccc;
      padding: 2px;
    }

  </style>
</head>
<body>


<div class="demo" id="d1">

  <h2 id="t1">handler</h2>

  <div id="c1">
    sem. Suspendisse porta felis ac ipsum. Sed tincidunt dui vitae nulla. Ut
    blandit. Nunc non neque. Mauris placerat. Vestibulum mollis tellus id dolor.
    Phasellus ac dolor molestie nunc euismod aliquam. Mauris tellus ipsum,
    fringilla id, tincidunt eu, vestibulum sit amet, metus. Quisque congue
    varius
    ligula. Quisque ornare mollis enim. Aliquam erat volutpat. Nulla mattis
    venenatis magna.
  </div>
</div>


<script>

  var animatedZippy;
  var animatedZippyHeaderEl;
  var propertyReplacer;

  function setUp() {
    animatedZippyHeaderEl = goog.dom.getElement('t1');
    animatedZippy = new goog.ui.AnimatedZippy(animatedZippyHeaderEl,
        goog.dom.getElement('c1'));

    propertyReplacer = new goog.testing.PropertyReplacer();
  }

  function tearDown() {
    propertyReplacer.reset();
    animatedZippy.dispose();
  }

  function testConstructor() {
    assertNotNull('must not be null', animatedZippy);
  }

  function testExpandCollapse() {
    var animationsPlayed = 0;
    var toggleEventsFired = 0;

    propertyReplacer.replace(goog.fx.Animation.prototype, 'play', function() {
      animationsPlayed++;
      this.dispatchAnimationEvent(goog.fx.Transition.EventType.END);
    });
    propertyReplacer.replace(goog.ui.AnimatedZippy.prototype, 'onAnimate_',
        goog.functions.NULL);

    goog.events.listenOnce(animatedZippy, goog.ui.Zippy.Events.TOGGLE,
        function(e) {
          toggleEventsFired++;
          assertTrue('TOGGLE event must be for expansion', e.expanded);
          assertEquals('expanded must be true', true,
              animatedZippy.isExpanded());
          assertEquals('aria-expanded must be true', 'true',
              goog.dom.a11y.getState(animatedZippyHeaderEl,
                  goog.dom.a11y.State.EXPANDED));
        });

    animatedZippy.expand();

    goog.events.listenOnce(animatedZippy, goog.ui.Zippy.Events.TOGGLE,
      function(e) {
        toggleEventsFired++;
        assertFalse('TOGGLE event must be for collapse', e.expanded);
        assertEquals('expanded must be false', false,
            animatedZippy.isExpanded());
        assertEquals('aria-expanded must be false', 'false',
            goog.dom.a11y.getState(animatedZippyHeaderEl,
                goog.dom.a11y.State.EXPANDED));
      });

    animatedZippy.collapse();

    assertEquals('animations must play', 2, animationsPlayed);
    assertEquals('TOGGLE events must fire', 2, toggleEventsFired);
  }

  function testExpandWhileAnimationRuns() {
    var animationStopper = function() {
      fail('expand() must replace animationStopper');
    };
    var animationsRunning = 0;

    propertyReplacer.replace(goog.fx.Animation.prototype, 'onPlay', function() {
      var that = this;
      animationStopper = function() {
        that.cycle(that.endTime);
      };
      animationsRunning++;
    });
    propertyReplacer.replace(goog.fx.Animation.prototype, 'onEnd', function() {
      animationsRunning--;
      this.dispatchAnimationEvent(goog.fx.Transition.EventType.END);
    });
    propertyReplacer.replace(goog.ui.AnimatedZippy.prototype, 'onAnimate_',
        goog.functions.NULL);

    animatedZippy.expand();
    animatedZippy.collapse();
    animatedZippy.expand();
    assertEquals('exactly 1 animation must be running', 1, animationsRunning);
    animationStopper();
    assertEquals('animation must have finished', 0, animationsRunning);
    assertEquals('expanded must be true', animatedZippy.isExpanded(), true);

    animatedZippy.collapse();
    animatedZippy.expand();
    animatedZippy.collapse();
    assertEquals('exactly 1 animation must be running', 1, animationsRunning);
    animationStopper();
    assertEquals('animation must have finished', 0, animationsRunning);
    assertEquals('expanded must be false', animatedZippy.isExpanded(), false);
  }

</script>

</body>
</html>