aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/ui/zippy_test.html
blob: 9c92e68f3302ba437c468ff311b479c20230d794 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<!DOCTYPE html>
<html>
<!--
Copyright 2008 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.Zippy</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.dom.classes');
    goog.require('goog.events');
    goog.require('goog.object');
    goog.require('goog.testing.events');
    goog.require('goog.testing.jsunit');
    goog.require('goog.ui.Zippy');
    goog.require('goog.ui.ZippyEvent');
    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 zippy, fakeZippy1, fakeZippy2, contentlessZippy, headerlessZippy;
  var lazyZippy;
  var lazyZippyCallCount;
  var lazyZippyContentEl;
  var dualHeaderZippy;
  var dualHeaderZippyCollapsedHeaderEl;
  var dualHeaderZippyExpandedHeaderEl;

  function setUp() {
    zippy = new goog.ui.Zippy(goog.dom.getElement('t1'),
        goog.dom.getElement('c1'));

    var fakeControlEl = document.createElement('button');
    var fakeContentEl = document.createElement('div');

    fakeZippy1 = new goog.ui.Zippy(fakeControlEl.cloneNode(true),
        fakeContentEl.cloneNode(true), true);
    fakeZippy2 = new goog.ui.Zippy(fakeControlEl.cloneNode(true),
        fakeContentEl.cloneNode(true), false);
    contentlessZippy = new goog.ui.Zippy(fakeControlEl.cloneNode(true),
        undefined, true);
    headerlessZippy = new goog.ui.Zippy(null, fakeContentEl.cloneNode(true),
        true);

    lazyZippyCallCount = 0;
    lazyZippyContentEl = fakeContentEl.cloneNode(true);
    lazyZippy = new goog.ui.Zippy(goog.dom.getElement('t1'), function() {
      lazyZippyCallCount++;
      return lazyZippyContentEl;
    });
    dualHeaderZippyCollapsedHeaderEl = fakeControlEl.cloneNode(true);
    dualHeaderZippyExpandedHeaderEl = fakeControlEl.cloneNode(true);
    dualHeaderZippy = new goog.ui.Zippy(dualHeaderZippyCollapsedHeaderEl,
        fakeContentEl.cloneNode(true), false, dualHeaderZippyExpandedHeaderEl);
  }

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

  function testIsExpanded() {
    assertEquals("Default expanded must be false", false, zippy.isExpanded());
    assertEquals("Expanded must be true", true, fakeZippy1.isExpanded());
    assertEquals("Expanded must be false", false, fakeZippy2.isExpanded());
    assertEquals("Expanded must be true", true, headerlessZippy.isExpanded());
    assertEquals("Expanded must be false", false, lazyZippy.isExpanded());
    assertEquals("Expanded must be false", false, dualHeaderZippy.isExpanded());
  }

  function tearDown() {
    zippy.dispose();
    fakeZippy1.dispose();
    fakeZippy2.dispose();
    contentlessZippy.dispose();
    headerlessZippy.dispose();
    lazyZippy.dispose();
    dualHeaderZippy.dispose();
  }

  function testExpandCollapse() {
    zippy.expand();
    headerlessZippy.expand();
    assertEquals("expanded must be true", true, zippy.isExpanded());
    assertEquals("expanded must be true", true, headerlessZippy.isExpanded());

    zippy.collapse();
    headerlessZippy.collapse();
    assertEquals("expanded must be false", false, zippy.isExpanded());
    assertEquals("expanded must be false", false, headerlessZippy.isExpanded());
  }

  function testExpandCollapse_lazyZippy() {
    assertEquals("callback should not be called #1.", 0, lazyZippyCallCount);
    lazyZippy.collapse();
    assertEquals("callback should not be called #2.", 0, lazyZippyCallCount);

    lazyZippy.expand();
    assertEquals("callback should be called once #1.", 1, lazyZippyCallCount);
    assertEquals("expanded must be true", true, lazyZippy.isExpanded());
    assertEquals("contentEl should be visible", "",
        lazyZippyContentEl.style.display);

    lazyZippy.collapse();
    assertEquals("callback should be called once #2.", 1, lazyZippyCallCount);
    assertEquals("expanded must be false", false, lazyZippy.isExpanded());
    assertEquals("contentEl should not be visible", "none",
        lazyZippyContentEl.style.display);

    lazyZippy.expand();
    assertEquals("callback should be called once #3.", 1, lazyZippyCallCount);
    assertEquals("expanded must be true #2", true, lazyZippy.isExpanded());
    assertEquals("contentEl should be visible #2", "",
        lazyZippyContentEl.style.display);
  }

  function testExpandCollapse_dualHeaderZippy() {
    dualHeaderZippy.expand();
    assertEquals("expanded must be true", true, dualHeaderZippy.isExpanded());
    assertFalse("collapsed header should not have state class name #1",
        hasCollapseOrExpandClasses(dualHeaderZippyCollapsedHeaderEl));
    assertFalse("expanded header should not have state class name #1",
        hasCollapseOrExpandClasses(dualHeaderZippyExpandedHeaderEl));

    dualHeaderZippy.collapse();
    assertEquals("expanded must be false", false, dualHeaderZippy.isExpanded());
    assertFalse("collapsed header should not have state class name #2",
        hasCollapseOrExpandClasses(dualHeaderZippyCollapsedHeaderEl));
    assertFalse("expanded header should not have state class name #2",
        hasCollapseOrExpandClasses(dualHeaderZippyExpandedHeaderEl));
  }

  function testSetExpand() {
    var expanded = !zippy.isExpanded();
    zippy.setExpanded(expanded);
    assertEquals("expanded must be " + expanded, expanded, zippy.isExpanded());
  }

  function testCssClassesAndAria() {
    assertTrue('goog-zippy-header is enabled',
        goog.dom.classes.has(zippy.elHeader_, 'goog-zippy-header'));
    assertEquals('header aria-expanded is false', 'false',
        goog.dom.a11y.getState(zippy.elHeader_, 'expanded'));
    zippy.setExpanded(true);
    assertTrue('goog-zippy-content is enabled',
        goog.dom.classes.has(zippy.getContentElement(), 'goog-zippy-content'));
    assertEquals('header aria role is TAB', 'tab',
        goog.dom.a11y.getRole(zippy.elHeader_));
    assertEquals('header aria-expanded is true', 'true',
        goog.dom.a11y.getState(zippy.elHeader_, 'expanded'));
  }

  function testHeaderTabIndex() {
    assertEquals('Header tabIndex is 0', 0, zippy.elHeader_.tabIndex);
  }

  function testGetVisibleHeaderElement() {
    dualHeaderZippy.setExpanded(false);
    assertEquals(dualHeaderZippyCollapsedHeaderEl,
        dualHeaderZippy.getVisibleHeaderElement());
    dualHeaderZippy.setExpanded(true);
    assertEquals(dualHeaderZippyExpandedHeaderEl,
        dualHeaderZippy.getVisibleHeaderElement())
  }

  function testToggle() {
    var expanded = !zippy.isExpanded();
    zippy.toggle();
    assertEquals("expanded must be " + expanded, expanded, zippy.isExpanded());
  }

  function testCustomEventTOGGLE() {
    var dispatchedActionCount;
    var handleAction = function() {
      dispatchedActionCount++;
    };

    var doTest = function (zippyObj) {
      dispatchedActionCount = 0;
      goog.events.listen(zippyObj, goog.ui.Zippy.Events.TOGGLE, handleAction);
      zippy.toggle();
      assertEquals("Custom Event must be called ", 1, dispatchedActionCount);
    };

    doTest(zippy);
    doTest(fakeZippy1);
    doTest(contentlessZippy);
    doTest(headerlessZippy);
  }

  function testActionEvent() {
    var actionEventCount = 0;
    var toggleEventCount = 0;
    var handleEvent = function(e) {
      if (e.type == goog.ui.Zippy.Events.TOGGLE) {
        toggleEventCount++;
      } else if (e.type == goog.ui.Zippy.Events.ACTION) {
        actionEventCount++;
        assertTrue('toggle must have been called first',
            toggleEventCount >= actionEventCount);
      }
    };
    goog.events.listen(zippy, goog.object.getValues(goog.ui.Zippy.Events),
        handleEvent);
    goog.testing.events.fireClickSequence(zippy.elHeader_);
    assertEquals('Zippy ACTION event fired', 1, actionEventCount);
    assertEquals('Zippy TOGGLE event fired', 1, toggleEventCount);

    zippy.toggle();
    assertEquals('Zippy ACTION event NOT fired', 1, actionEventCount);
    assertEquals('Zippy TOGGLE event fired', 2, toggleEventCount);
  }

  function testBasicZippyBehavior() {
    var dispatchedActionCount = 0;
    var handleAction = function() {
      dispatchedActionCount++;
    };

    goog.events.listen(zippy, goog.ui.Zippy.Events.TOGGLE, handleAction);
    goog.testing.events.fireClickSequence(zippy.elHeader_);
    assertEquals('Zippy  must have dispatched TOGGLE on click', 1,
        dispatchedActionCount);

  }

  function hasCollapseOrExpandClasses(el) {
    var isCollapsed =  goog.dom.classes.has(el, 'goog-zippy-collapsed');
    var isExpanded =  goog.dom.classes.has(el, 'goog-zippy-expanded');
    return isCollapsed || isExpanded;
  }

</script>

</body>
</html>