123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- // Copyright 2008 The Closure Library Authors. All Rights Reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS-IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- goog.provide('goog.ui.ZippyTest');
- goog.setTestOnly('goog.ui.ZippyTest');
- goog.require('goog.a11y.aria');
- goog.require('goog.dom');
- goog.require('goog.dom.TagName');
- goog.require('goog.dom.classlist');
- goog.require('goog.events');
- goog.require('goog.events.KeyCodes');
- goog.require('goog.object');
- goog.require('goog.testing.events');
- goog.require('goog.testing.jsunit');
- goog.require('goog.ui.Zippy');
- 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 = goog.dom.createElement(goog.dom.TagName.BUTTON);
- var fakeContentEl = goog.dom.createElement(goog.dom.TagName.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.classlist.contains(zippy.elHeader_, 'goog-zippy-header'));
- assertNotNull(zippy.elHeader_);
- assertEquals(
- 'header aria-expanded is false', 'false',
- goog.a11y.aria.getState(zippy.elHeader_, 'expanded'));
- zippy.setExpanded(true);
- assertTrue(
- 'goog-zippy-content is enabled',
- goog.dom.classlist.contains(
- zippy.getContentElement(), 'goog-zippy-content'));
- assertEquals(
- 'header aria role is TAB', 'tab',
- goog.a11y.aria.getRole(zippy.elHeader_));
- assertEquals(
- 'header aria-expanded is true', 'true',
- goog.a11y.aria.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.classlist.contains(el, 'goog-zippy-collapsed');
- var isExpanded = goog.dom.classlist.contains(el, 'goog-zippy-expanded');
- return isCollapsed || isExpanded;
- }
- function testIsHandleKeyEvent() {
- zippy.setHandleKeyboardEvents(false);
- assertFalse('Zippy is not handling key events', zippy.isHandleKeyEvents());
- assertTrue(
- 'Zippy setHandleKeyEvents does not affect handling mouse events',
- zippy.isHandleMouseEvents());
- assertEquals(0, zippy.keyboardEventHandler_.getListenerCount());
- zippy.setHandleKeyboardEvents(true);
- assertTrue('Zippy is handling key events', zippy.isHandleKeyEvents());
- assertTrue(
- 'Zippy setHandleKeyEvents does not affect handling mouse events',
- zippy.isHandleMouseEvents());
- assertNotEquals(0, zippy.keyboardEventHandler_.getListenerCount());
- }
- function testIsHandleMouseEvent() {
- zippy.setHandleMouseEvents(false);
- assertFalse(
- 'Zippy is not handling mouse events', zippy.isHandleMouseEvents());
- assertTrue(
- 'Zippy setHandleMouseEvents does not affect handling key events',
- zippy.isHandleKeyEvents());
- assertEquals(0, zippy.mouseEventHandler_.getListenerCount());
- zippy.setHandleMouseEvents(true);
- assertTrue('Zippy is handling mouse events', zippy.isHandleMouseEvents());
- assertTrue(
- 'Zippy setHandleMouseEvents does not affect handling key events',
- zippy.isHandleKeyEvents());
- assertNotEquals(0, zippy.mouseEventHandler_.getListenerCount());
- }
- function testKeyDownEventTriggersHeader() {
- 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);
- }
- };
- zippy.setHandleKeyboardEvents(true);
- goog.events.listen(
- zippy, goog.object.getValues(goog.ui.Zippy.Events), handleEvent);
- goog.testing.events.fireKeySequence(
- zippy.elHeader_, goog.events.KeyCodes.SPACE);
- assertEquals('Zippy ACTION event fired', 1, actionEventCount);
- assertEquals('Zippy TOGGLE event fired', 1, toggleEventCount);
- }
|