| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.ui.ZippyTest'); |
| 16 |
goog.setTestOnly('goog.ui.ZippyTest'); |
| 17 |
|
| 18 |
goog.require('goog.a11y.aria'); |
| 19 |
goog.require('goog.dom'); |
| 20 |
goog.require('goog.dom.TagName'); |
| 21 |
goog.require('goog.dom.classlist'); |
| 22 |
goog.require('goog.events'); |
| 23 |
goog.require('goog.events.KeyCodes'); |
| 24 |
goog.require('goog.object'); |
| 25 |
goog.require('goog.testing.events'); |
| 26 |
goog.require('goog.testing.jsunit'); |
| 27 |
goog.require('goog.ui.Zippy'); |
| 28 |
|
| 29 |
var zippy, fakeZippy1, fakeZippy2, contentlessZippy, headerlessZippy, |
| 30 |
buttonZippy; |
| 31 |
var lazyZippy; |
| 32 |
var lazyZippyCallCount; |
| 33 |
var lazyZippyContentEl; |
| 34 |
var dualHeaderZippy; |
| 35 |
var dualHeaderZippyCollapsedHeaderEl; |
| 36 |
var dualHeaderZippyExpandedHeaderEl; |
| 37 |
|
| 38 |
function setUp() { |
| 39 |
zippy = |
| 40 |
new goog.ui.Zippy(goog.dom.getElement('t1'), goog.dom.getElement('c1')); |
| 41 |
|
| 42 |
var fakeControlEl = goog.dom.createElement(goog.dom.TagName.BUTTON); |
| 43 |
var fakeContentEl = goog.dom.createElement(goog.dom.TagName.DIV); |
| 44 |
|
| 45 |
fakeZippy1 = new goog.ui.Zippy( |
| 46 |
fakeControlEl.cloneNode(true), fakeContentEl.cloneNode(true), true); |
| 47 |
fakeZippy2 = new goog.ui.Zippy( |
| 48 |
fakeControlEl.cloneNode(true), fakeContentEl.cloneNode(true), false); |
| 49 |
contentlessZippy = |
| 50 |
new goog.ui.Zippy(fakeControlEl.cloneNode(true), undefined, true); |
| 51 |
headerlessZippy = |
| 52 |
new goog.ui.Zippy(null, fakeContentEl.cloneNode(true), true); |
| 53 |
buttonZippy = new goog.ui.Zippy( |
| 54 |
null, fakeContentEl.cloneNode(true), true, null, null, |
| 55 |
goog.a11y.aria.Role.BUTTON); |
| 56 |
|
| 57 |
lazyZippyCallCount = 0; |
| 58 |
lazyZippyContentEl = fakeContentEl.cloneNode(true); |
| 59 |
lazyZippy = new goog.ui.Zippy(goog.dom.getElement('t1'), function() { |
| 60 |
lazyZippyCallCount++; |
| 61 |
return lazyZippyContentEl; |
| 62 |
}); |
| 63 |
dualHeaderZippyCollapsedHeaderEl = fakeControlEl.cloneNode(true); |
| 64 |
dualHeaderZippyExpandedHeaderEl = fakeControlEl.cloneNode(true); |
| 65 |
dualHeaderZippy = new goog.ui.Zippy( |
| 66 |
dualHeaderZippyCollapsedHeaderEl, fakeContentEl.cloneNode(true), false, |
| 67 |
dualHeaderZippyExpandedHeaderEl); |
| 68 |
} |
| 69 |
|
| 70 |
function testConstructor() { |
| 71 |
assertNotNull('must not be null', zippy); |
| 72 |
} |
| 73 |
|
| 74 |
function testIsExpanded() { |
| 75 |
assertEquals('Default expanded must be false', false, zippy.isExpanded()); |
| 76 |
assertEquals('Expanded must be true', true, fakeZippy1.isExpanded()); |
| 77 |
assertEquals('Expanded must be false', false, fakeZippy2.isExpanded()); |
| 78 |
assertEquals('Expanded must be true', true, headerlessZippy.isExpanded()); |
| 79 |
assertEquals('Expanded must be false', false, lazyZippy.isExpanded()); |
| 80 |
assertEquals('Expanded must be false', false, dualHeaderZippy.isExpanded()); |
| 81 |
assertEquals('Expanded must be true', true, buttonZippy.isExpanded()); |
| 82 |
} |
| 83 |
|
| 84 |
function tearDown() { |
| 85 |
zippy.dispose(); |
| 86 |
fakeZippy1.dispose(); |
| 87 |
fakeZippy2.dispose(); |
| 88 |
contentlessZippy.dispose(); |
| 89 |
headerlessZippy.dispose(); |
| 90 |
lazyZippy.dispose(); |
| 91 |
dualHeaderZippy.dispose(); |
| 92 |
buttonZippy.dispose(); |
| 93 |
} |
| 94 |
|
| 95 |
function testExpandCollapse() { |
| 96 |
zippy.expand(); |
| 97 |
headerlessZippy.expand(); |
| 98 |
assertEquals('expanded must be true', true, zippy.isExpanded()); |
| 99 |
assertEquals('expanded must be true', true, headerlessZippy.isExpanded()); |
| 100 |
|
| 101 |
zippy.collapse(); |
| 102 |
headerlessZippy.collapse(); |
| 103 |
assertEquals('expanded must be false', false, zippy.isExpanded()); |
| 104 |
assertEquals('expanded must be false', false, headerlessZippy.isExpanded()); |
| 105 |
} |
| 106 |
|
| 107 |
function testExpandCollapse_lazyZippy() { |
| 108 |
assertEquals('callback should not be called #1.', 0, lazyZippyCallCount); |
| 109 |
lazyZippy.collapse(); |
| 110 |
assertEquals('callback should not be called #2.', 0, lazyZippyCallCount); |
| 111 |
|
| 112 |
lazyZippy.expand(); |
| 113 |
assertEquals('callback should be called once #1.', 1, lazyZippyCallCount); |
| 114 |
assertEquals('expanded must be true', true, lazyZippy.isExpanded()); |
| 115 |
assertEquals( |
| 116 |
'contentEl should be visible', '', lazyZippyContentEl.style.display); |
| 117 |
|
| 118 |
lazyZippy.collapse(); |
| 119 |
assertEquals('callback should be called once #2.', 1, lazyZippyCallCount); |
| 120 |
assertEquals('expanded must be false', false, lazyZippy.isExpanded()); |
| 121 |
assertEquals( |
| 122 |
'contentEl should not be visible', 'none', |
| 123 |
lazyZippyContentEl.style.display); |
| 124 |
|
| 125 |
lazyZippy.expand(); |
| 126 |
assertEquals('callback should be called once #3.', 1, lazyZippyCallCount); |
| 127 |
assertEquals('expanded must be true #2', true, lazyZippy.isExpanded()); |
| 128 |
assertEquals( |
| 129 |
'contentEl should be visible #2', '', lazyZippyContentEl.style.display); |
| 130 |
} |
| 131 |
|
| 132 |
function testExpandCollapse_dualHeaderZippy() { |
| 133 |
dualHeaderZippy.expand(); |
| 134 |
assertEquals('expanded must be true', true, dualHeaderZippy.isExpanded()); |
| 135 |
assertFalse( |
| 136 |
'collapsed header should not have state class name #1', |
| 137 |
hasCollapseOrExpandClasses(dualHeaderZippyCollapsedHeaderEl)); |
| 138 |
assertFalse( |
| 139 |
'expanded header should not have state class name #1', |
| 140 |
hasCollapseOrExpandClasses(dualHeaderZippyExpandedHeaderEl)); |
| 141 |
|
| 142 |
dualHeaderZippy.collapse(); |
| 143 |
assertEquals('expanded must be false', false, dualHeaderZippy.isExpanded()); |
| 144 |
assertFalse( |
| 145 |
'collapsed header should not have state class name #2', |
| 146 |
hasCollapseOrExpandClasses(dualHeaderZippyCollapsedHeaderEl)); |
| 147 |
assertFalse( |
| 148 |
'expanded header should not have state class name #2', |
| 149 |
hasCollapseOrExpandClasses(dualHeaderZippyExpandedHeaderEl)); |
| 150 |
} |
| 151 |
|
| 152 |
function testSetExpand() { |
| 153 |
var expanded = !zippy.isExpanded(); |
| 154 |
zippy.setExpanded(expanded); |
| 155 |
assertEquals('expanded must be ' + expanded, expanded, zippy.isExpanded()); |
| 156 |
} |
| 157 |
|
| 158 |
function testCssClassesAndAria() { |
| 159 |
assertTrue( |
| 160 |
'goog-zippy-header is enabled', |
| 161 |
goog.dom.classlist.contains(zippy.elHeader_, 'goog-zippy-header')); |
| 162 |
assertNotNull(zippy.elHeader_); |
| 163 |
assertEquals( |
| 164 |
'header aria-expanded is false', 'false', |
| 165 |
goog.a11y.aria.getState(zippy.elHeader_, 'expanded')); |
| 166 |
zippy.setExpanded(true); |
| 167 |
assertTrue( |
| 168 |
'goog-zippy-content is enabled', |
| 169 |
goog.dom.classlist.contains( |
| 170 |
zippy.getContentElement(), 'goog-zippy-content')); |
| 171 |
assertEquals( |
| 172 |
'header aria role is TAB', 'tab', |
| 173 |
goog.a11y.aria.getRole(zippy.elHeader_)); |
| 174 |
assertEquals( |
| 175 |
'header aria-expanded is true', 'true', |
| 176 |
goog.a11y.aria.getState(zippy.elHeader_, 'expanded')); |
| 177 |
} |
| 178 |
|
| 179 |
function testAreaRoleOverride() { |
| 180 |
assertEquals( |
| 181 |
'Aria role override is goog.a11y.aria.Role.BUTTON', |
| 182 |
goog.a11y.aria.Role.BUTTON, buttonZippy.getAriaRole()); |
| 183 |
} |
| 184 |
|
| 185 |
function testHeaderTabIndex() { |
| 186 |
assertEquals('Header tabIndex is 0', 0, zippy.elHeader_.tabIndex); |
| 187 |
} |
| 188 |
|
| 189 |
function testGetVisibleHeaderElement() { |
| 190 |
dualHeaderZippy.setExpanded(false); |
| 191 |
assertEquals( |
| 192 |
dualHeaderZippyCollapsedHeaderEl, |
| 193 |
dualHeaderZippy.getVisibleHeaderElement()); |
| 194 |
dualHeaderZippy.setExpanded(true); |
| 195 |
assertEquals( |
| 196 |
dualHeaderZippyExpandedHeaderEl, |
| 197 |
dualHeaderZippy.getVisibleHeaderElement()); |
| 198 |
} |
| 199 |
|
| 200 |
function testToggle() { |
| 201 |
var expanded = !zippy.isExpanded(); |
| 202 |
zippy.toggle(); |
| 203 |
assertEquals('expanded must be ' + expanded, expanded, zippy.isExpanded()); |
| 204 |
} |
| 205 |
|
| 206 |
function testCustomEventTOGGLE() { |
| 207 |
var dispatchedActionCount; |
| 208 |
var handleAction = function() { dispatchedActionCount++; }; |
| 209 |
|
| 210 |
var doTest = function(zippyObj) { |
| 211 |
dispatchedActionCount = 0; |
| 212 |
goog.events.listen(zippyObj, goog.ui.Zippy.Events.TOGGLE, handleAction); |
| 213 |
zippy.toggle(); |
| 214 |
assertEquals('Custom Event must be called ', 1, dispatchedActionCount); |
| 215 |
}; |
| 216 |
|
| 217 |
doTest(zippy); |
| 218 |
doTest(fakeZippy1); |
| 219 |
doTest(contentlessZippy); |
| 220 |
doTest(headerlessZippy); |
| 221 |
} |
| 222 |
|
| 223 |
function testActionEvent() { |
| 224 |
var actionEventCount = 0; |
| 225 |
var toggleEventCount = 0; |
| 226 |
var handleEvent = function(e) { |
| 227 |
if (e.type == goog.ui.Zippy.Events.TOGGLE) { |
| 228 |
toggleEventCount++; |
| 229 |
} else if (e.type == goog.ui.Zippy.Events.ACTION) { |
| 230 |
actionEventCount++; |
| 231 |
assertNotNull( |
| 232 |
'action event must have triggering event', e.triggeringEvent); |
| 233 |
assertTrue( |
| 234 |
'toggle must have been called first', |
| 235 |
toggleEventCount >= actionEventCount); |
| 236 |
} |
| 237 |
}; |
| 238 |
goog.events.listen( |
| 239 |
zippy, goog.object.getValues(goog.ui.Zippy.Events), handleEvent); |
| 240 |
goog.testing.events.fireClickSequence(zippy.elHeader_); |
| 241 |
assertEquals('Zippy ACTION event fired', 1, actionEventCount); |
| 242 |
assertEquals('Zippy TOGGLE event fired', 1, toggleEventCount); |
| 243 |
|
| 244 |
zippy.toggle(); |
| 245 |
assertEquals('Zippy ACTION event NOT fired', 1, actionEventCount); |
| 246 |
assertEquals('Zippy TOGGLE event fired', 2, toggleEventCount); |
| 247 |
} |
| 248 |
|
| 249 |
function testBasicZippyBehavior() { |
| 250 |
var dispatchedActionCount = 0; |
| 251 |
var handleAction = function() { dispatchedActionCount++; }; |
| 252 |
|
| 253 |
goog.events.listen(zippy, goog.ui.Zippy.Events.TOGGLE, handleAction); |
| 254 |
goog.testing.events.fireClickSequence(zippy.elHeader_); |
| 255 |
assertEquals( |
| 256 |
'Zippy must have dispatched TOGGLE on click', 1, dispatchedActionCount); |
| 257 |
} |
| 258 |
|
| 259 |
function hasCollapseOrExpandClasses(el) { |
| 260 |
var isCollapsed = goog.dom.classlist.contains(el, 'goog-zippy-collapsed'); |
| 261 |
var isExpanded = goog.dom.classlist.contains(el, 'goog-zippy-expanded'); |
| 262 |
return isCollapsed || isExpanded; |
| 263 |
} |
| 264 |
|
| 265 |
function testIsHandleKeyEvent() { |
| 266 |
zippy.setHandleKeyboardEvents(false); |
| 267 |
assertFalse('Zippy is not handling key events', zippy.isHandleKeyEvents()); |
| 268 |
assertTrue( |
| 269 |
'Zippy setHandleKeyEvents does not affect handling mouse events', |
| 270 |
zippy.isHandleMouseEvents()); |
| 271 |
assertEquals(0, zippy.keyboardEventHandler_.getListenerCount()); |
| 272 |
|
| 273 |
zippy.setHandleKeyboardEvents(true); |
| 274 |
assertTrue('Zippy is handling key events', zippy.isHandleKeyEvents()); |
| 275 |
assertTrue( |
| 276 |
'Zippy setHandleKeyEvents does not affect handling mouse events', |
| 277 |
zippy.isHandleMouseEvents()); |
| 278 |
assertNotEquals(0, zippy.keyboardEventHandler_.getListenerCount()); |
| 279 |
} |
| 280 |
|
| 281 |
function testIsHandleMouseEvent() { |
| 282 |
zippy.setHandleMouseEvents(false); |
| 283 |
assertFalse( |
| 284 |
'Zippy is not handling mouse events', zippy.isHandleMouseEvents()); |
| 285 |
assertTrue( |
| 286 |
'Zippy setHandleMouseEvents does not affect handling key events', |
| 287 |
zippy.isHandleKeyEvents()); |
| 288 |
assertEquals(0, zippy.mouseEventHandler_.getListenerCount()); |
| 289 |
|
| 290 |
zippy.setHandleMouseEvents(true); |
| 291 |
assertTrue('Zippy is handling mouse events', zippy.isHandleMouseEvents()); |
| 292 |
assertTrue( |
| 293 |
'Zippy setHandleMouseEvents does not affect handling key events', |
| 294 |
zippy.isHandleKeyEvents()); |
| 295 |
assertNotEquals(0, zippy.mouseEventHandler_.getListenerCount()); |
| 296 |
} |
| 297 |
|
| 298 |
function testKeyDownEventTriggersHeader() { |
| 299 |
var actionEventCount = 0; |
| 300 |
var toggleEventCount = 0; |
| 301 |
var handleEvent = function(e) { |
| 302 |
if (e.type == goog.ui.Zippy.Events.TOGGLE) { |
| 303 |
toggleEventCount++; |
| 304 |
} else if (e.type == goog.ui.Zippy.Events.ACTION) { |
| 305 |
actionEventCount++; |
| 306 |
assertTrue( |
| 307 |
'toggle must have been called first', |
| 308 |
toggleEventCount >= actionEventCount); |
| 309 |
} |
| 310 |
}; |
| 311 |
zippy.setHandleKeyboardEvents(true); |
| 312 |
goog.events.listen( |
| 313 |
zippy, goog.object.getValues(goog.ui.Zippy.Events), handleEvent); |
| 314 |
|
| 315 |
goog.testing.events.fireKeySequence( |
| 316 |
zippy.elHeader_, goog.events.KeyCodes.SPACE); |
| 317 |
|
| 318 |
assertEquals('Zippy ACTION event fired', 1, actionEventCount); |
| 319 |
assertEquals('Zippy TOGGLE event fired', 1, toggleEventCount); |
| 320 |
} |