zippy_test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. goog.provide('goog.ui.ZippyTest');
  15. goog.setTestOnly('goog.ui.ZippyTest');
  16. goog.require('goog.a11y.aria');
  17. goog.require('goog.dom');
  18. goog.require('goog.dom.TagName');
  19. goog.require('goog.dom.classlist');
  20. goog.require('goog.events');
  21. goog.require('goog.events.KeyCodes');
  22. goog.require('goog.object');
  23. goog.require('goog.testing.events');
  24. goog.require('goog.testing.jsunit');
  25. goog.require('goog.ui.Zippy');
  26. var zippy, fakeZippy1, fakeZippy2, contentlessZippy, headerlessZippy;
  27. var lazyZippy;
  28. var lazyZippyCallCount;
  29. var lazyZippyContentEl;
  30. var dualHeaderZippy;
  31. var dualHeaderZippyCollapsedHeaderEl;
  32. var dualHeaderZippyExpandedHeaderEl;
  33. function setUp() {
  34. zippy =
  35. new goog.ui.Zippy(goog.dom.getElement('t1'), goog.dom.getElement('c1'));
  36. var fakeControlEl = goog.dom.createElement(goog.dom.TagName.BUTTON);
  37. var fakeContentEl = goog.dom.createElement(goog.dom.TagName.DIV);
  38. fakeZippy1 = new goog.ui.Zippy(
  39. fakeControlEl.cloneNode(true), fakeContentEl.cloneNode(true), true);
  40. fakeZippy2 = new goog.ui.Zippy(
  41. fakeControlEl.cloneNode(true), fakeContentEl.cloneNode(true), false);
  42. contentlessZippy =
  43. new goog.ui.Zippy(fakeControlEl.cloneNode(true), undefined, true);
  44. headerlessZippy =
  45. new goog.ui.Zippy(null, fakeContentEl.cloneNode(true), true);
  46. lazyZippyCallCount = 0;
  47. lazyZippyContentEl = fakeContentEl.cloneNode(true);
  48. lazyZippy = new goog.ui.Zippy(goog.dom.getElement('t1'), function() {
  49. lazyZippyCallCount++;
  50. return lazyZippyContentEl;
  51. });
  52. dualHeaderZippyCollapsedHeaderEl = fakeControlEl.cloneNode(true);
  53. dualHeaderZippyExpandedHeaderEl = fakeControlEl.cloneNode(true);
  54. dualHeaderZippy = new goog.ui.Zippy(
  55. dualHeaderZippyCollapsedHeaderEl, fakeContentEl.cloneNode(true), false,
  56. dualHeaderZippyExpandedHeaderEl);
  57. }
  58. function testConstructor() {
  59. assertNotNull('must not be null', zippy);
  60. }
  61. function testIsExpanded() {
  62. assertEquals('Default expanded must be false', false, zippy.isExpanded());
  63. assertEquals('Expanded must be true', true, fakeZippy1.isExpanded());
  64. assertEquals('Expanded must be false', false, fakeZippy2.isExpanded());
  65. assertEquals('Expanded must be true', true, headerlessZippy.isExpanded());
  66. assertEquals('Expanded must be false', false, lazyZippy.isExpanded());
  67. assertEquals('Expanded must be false', false, dualHeaderZippy.isExpanded());
  68. }
  69. function tearDown() {
  70. zippy.dispose();
  71. fakeZippy1.dispose();
  72. fakeZippy2.dispose();
  73. contentlessZippy.dispose();
  74. headerlessZippy.dispose();
  75. lazyZippy.dispose();
  76. dualHeaderZippy.dispose();
  77. }
  78. function testExpandCollapse() {
  79. zippy.expand();
  80. headerlessZippy.expand();
  81. assertEquals('expanded must be true', true, zippy.isExpanded());
  82. assertEquals('expanded must be true', true, headerlessZippy.isExpanded());
  83. zippy.collapse();
  84. headerlessZippy.collapse();
  85. assertEquals('expanded must be false', false, zippy.isExpanded());
  86. assertEquals('expanded must be false', false, headerlessZippy.isExpanded());
  87. }
  88. function testExpandCollapse_lazyZippy() {
  89. assertEquals('callback should not be called #1.', 0, lazyZippyCallCount);
  90. lazyZippy.collapse();
  91. assertEquals('callback should not be called #2.', 0, lazyZippyCallCount);
  92. lazyZippy.expand();
  93. assertEquals('callback should be called once #1.', 1, lazyZippyCallCount);
  94. assertEquals('expanded must be true', true, lazyZippy.isExpanded());
  95. assertEquals(
  96. 'contentEl should be visible', '', lazyZippyContentEl.style.display);
  97. lazyZippy.collapse();
  98. assertEquals('callback should be called once #2.', 1, lazyZippyCallCount);
  99. assertEquals('expanded must be false', false, lazyZippy.isExpanded());
  100. assertEquals(
  101. 'contentEl should not be visible', 'none',
  102. lazyZippyContentEl.style.display);
  103. lazyZippy.expand();
  104. assertEquals('callback should be called once #3.', 1, lazyZippyCallCount);
  105. assertEquals('expanded must be true #2', true, lazyZippy.isExpanded());
  106. assertEquals(
  107. 'contentEl should be visible #2', '', lazyZippyContentEl.style.display);
  108. }
  109. function testExpandCollapse_dualHeaderZippy() {
  110. dualHeaderZippy.expand();
  111. assertEquals('expanded must be true', true, dualHeaderZippy.isExpanded());
  112. assertFalse(
  113. 'collapsed header should not have state class name #1',
  114. hasCollapseOrExpandClasses(dualHeaderZippyCollapsedHeaderEl));
  115. assertFalse(
  116. 'expanded header should not have state class name #1',
  117. hasCollapseOrExpandClasses(dualHeaderZippyExpandedHeaderEl));
  118. dualHeaderZippy.collapse();
  119. assertEquals('expanded must be false', false, dualHeaderZippy.isExpanded());
  120. assertFalse(
  121. 'collapsed header should not have state class name #2',
  122. hasCollapseOrExpandClasses(dualHeaderZippyCollapsedHeaderEl));
  123. assertFalse(
  124. 'expanded header should not have state class name #2',
  125. hasCollapseOrExpandClasses(dualHeaderZippyExpandedHeaderEl));
  126. }
  127. function testSetExpand() {
  128. var expanded = !zippy.isExpanded();
  129. zippy.setExpanded(expanded);
  130. assertEquals('expanded must be ' + expanded, expanded, zippy.isExpanded());
  131. }
  132. function testCssClassesAndAria() {
  133. assertTrue(
  134. 'goog-zippy-header is enabled',
  135. goog.dom.classlist.contains(zippy.elHeader_, 'goog-zippy-header'));
  136. assertNotNull(zippy.elHeader_);
  137. assertEquals(
  138. 'header aria-expanded is false', 'false',
  139. goog.a11y.aria.getState(zippy.elHeader_, 'expanded'));
  140. zippy.setExpanded(true);
  141. assertTrue(
  142. 'goog-zippy-content is enabled',
  143. goog.dom.classlist.contains(
  144. zippy.getContentElement(), 'goog-zippy-content'));
  145. assertEquals(
  146. 'header aria role is TAB', 'tab',
  147. goog.a11y.aria.getRole(zippy.elHeader_));
  148. assertEquals(
  149. 'header aria-expanded is true', 'true',
  150. goog.a11y.aria.getState(zippy.elHeader_, 'expanded'));
  151. }
  152. function testHeaderTabIndex() {
  153. assertEquals('Header tabIndex is 0', 0, zippy.elHeader_.tabIndex);
  154. }
  155. function testGetVisibleHeaderElement() {
  156. dualHeaderZippy.setExpanded(false);
  157. assertEquals(
  158. dualHeaderZippyCollapsedHeaderEl,
  159. dualHeaderZippy.getVisibleHeaderElement());
  160. dualHeaderZippy.setExpanded(true);
  161. assertEquals(
  162. dualHeaderZippyExpandedHeaderEl,
  163. dualHeaderZippy.getVisibleHeaderElement());
  164. }
  165. function testToggle() {
  166. var expanded = !zippy.isExpanded();
  167. zippy.toggle();
  168. assertEquals('expanded must be ' + expanded, expanded, zippy.isExpanded());
  169. }
  170. function testCustomEventTOGGLE() {
  171. var dispatchedActionCount;
  172. var handleAction = function() { dispatchedActionCount++; };
  173. var doTest = function(zippyObj) {
  174. dispatchedActionCount = 0;
  175. goog.events.listen(zippyObj, goog.ui.Zippy.Events.TOGGLE, handleAction);
  176. zippy.toggle();
  177. assertEquals('Custom Event must be called ', 1, dispatchedActionCount);
  178. };
  179. doTest(zippy);
  180. doTest(fakeZippy1);
  181. doTest(contentlessZippy);
  182. doTest(headerlessZippy);
  183. }
  184. function testActionEvent() {
  185. var actionEventCount = 0;
  186. var toggleEventCount = 0;
  187. var handleEvent = function(e) {
  188. if (e.type == goog.ui.Zippy.Events.TOGGLE) {
  189. toggleEventCount++;
  190. } else if (e.type == goog.ui.Zippy.Events.ACTION) {
  191. actionEventCount++;
  192. assertTrue(
  193. 'toggle must have been called first',
  194. toggleEventCount >= actionEventCount);
  195. }
  196. };
  197. goog.events.listen(
  198. zippy, goog.object.getValues(goog.ui.Zippy.Events), handleEvent);
  199. goog.testing.events.fireClickSequence(zippy.elHeader_);
  200. assertEquals('Zippy ACTION event fired', 1, actionEventCount);
  201. assertEquals('Zippy TOGGLE event fired', 1, toggleEventCount);
  202. zippy.toggle();
  203. assertEquals('Zippy ACTION event NOT fired', 1, actionEventCount);
  204. assertEquals('Zippy TOGGLE event fired', 2, toggleEventCount);
  205. }
  206. function testBasicZippyBehavior() {
  207. var dispatchedActionCount = 0;
  208. var handleAction = function() { dispatchedActionCount++; };
  209. goog.events.listen(zippy, goog.ui.Zippy.Events.TOGGLE, handleAction);
  210. goog.testing.events.fireClickSequence(zippy.elHeader_);
  211. assertEquals(
  212. 'Zippy must have dispatched TOGGLE on click', 1, dispatchedActionCount);
  213. }
  214. function hasCollapseOrExpandClasses(el) {
  215. var isCollapsed = goog.dom.classlist.contains(el, 'goog-zippy-collapsed');
  216. var isExpanded = goog.dom.classlist.contains(el, 'goog-zippy-expanded');
  217. return isCollapsed || isExpanded;
  218. }
  219. function testIsHandleKeyEvent() {
  220. zippy.setHandleKeyboardEvents(false);
  221. assertFalse('Zippy is not handling key events', zippy.isHandleKeyEvents());
  222. assertTrue(
  223. 'Zippy setHandleKeyEvents does not affect handling mouse events',
  224. zippy.isHandleMouseEvents());
  225. assertEquals(0, zippy.keyboardEventHandler_.getListenerCount());
  226. zippy.setHandleKeyboardEvents(true);
  227. assertTrue('Zippy is handling key events', zippy.isHandleKeyEvents());
  228. assertTrue(
  229. 'Zippy setHandleKeyEvents does not affect handling mouse events',
  230. zippy.isHandleMouseEvents());
  231. assertNotEquals(0, zippy.keyboardEventHandler_.getListenerCount());
  232. }
  233. function testIsHandleMouseEvent() {
  234. zippy.setHandleMouseEvents(false);
  235. assertFalse(
  236. 'Zippy is not handling mouse events', zippy.isHandleMouseEvents());
  237. assertTrue(
  238. 'Zippy setHandleMouseEvents does not affect handling key events',
  239. zippy.isHandleKeyEvents());
  240. assertEquals(0, zippy.mouseEventHandler_.getListenerCount());
  241. zippy.setHandleMouseEvents(true);
  242. assertTrue('Zippy is handling mouse events', zippy.isHandleMouseEvents());
  243. assertTrue(
  244. 'Zippy setHandleMouseEvents does not affect handling key events',
  245. zippy.isHandleKeyEvents());
  246. assertNotEquals(0, zippy.mouseEventHandler_.getListenerCount());
  247. }
  248. function testKeyDownEventTriggersHeader() {
  249. var actionEventCount = 0;
  250. var toggleEventCount = 0;
  251. var handleEvent = function(e) {
  252. if (e.type == goog.ui.Zippy.Events.TOGGLE) {
  253. toggleEventCount++;
  254. } else if (e.type == goog.ui.Zippy.Events.ACTION) {
  255. actionEventCount++;
  256. assertTrue(
  257. 'toggle must have been called first',
  258. toggleEventCount >= actionEventCount);
  259. }
  260. };
  261. zippy.setHandleKeyboardEvents(true);
  262. goog.events.listen(
  263. zippy, goog.object.getValues(goog.ui.Zippy.Events), handleEvent);
  264. goog.testing.events.fireKeySequence(
  265. zippy.elHeader_, goog.events.KeyCodes.SPACE);
  266. assertEquals('Zippy ACTION event fired', 1, actionEventCount);
  267. assertEquals('Zippy TOGGLE event fired', 1, toggleEventCount);
  268. }