palette_test.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright 2012 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.PaletteTest');
  15. goog.setTestOnly('goog.ui.PaletteTest');
  16. goog.require('goog.a11y.aria');
  17. goog.require('goog.dom');
  18. goog.require('goog.events');
  19. goog.require('goog.events.EventType');
  20. goog.require('goog.events.KeyCodes');
  21. goog.require('goog.events.KeyEvent');
  22. goog.require('goog.testing.events.Event');
  23. goog.require('goog.testing.jsunit');
  24. goog.require('goog.testing.recordFunction');
  25. goog.require('goog.ui.Component');
  26. goog.require('goog.ui.Container');
  27. goog.require('goog.ui.Palette');
  28. var palette;
  29. var nodes;
  30. function setUp() {
  31. nodes = [];
  32. for (var i = 0; i < 23; i++) {
  33. var node = goog.dom.createTextNode('node[' + i + ']');
  34. nodes.push(node);
  35. }
  36. palette = new goog.ui.Palette(nodes);
  37. }
  38. function tearDown() {
  39. palette.dispose();
  40. goog.dom.removeChildren(document.getElementById('sandbox'));
  41. }
  42. function testAfterHighlightListener() {
  43. palette.setHighlightedIndex(0);
  44. var handler = new goog.testing.recordFunction();
  45. goog.events.listen(
  46. palette, goog.ui.Palette.EventType.AFTER_HIGHLIGHT, handler);
  47. palette.setHighlightedIndex(2);
  48. assertEquals(1, handler.getCallCount());
  49. palette.setHighlightedIndex(-1);
  50. assertEquals(2, handler.getCallCount());
  51. }
  52. function testHighlightItemUpdatesParentA11yActiveDescendant() {
  53. var container = new goog.ui.Container();
  54. container.render(document.getElementById('sandbox'));
  55. container.addChild(palette, true);
  56. palette.setHighlightedItem(nodes[0]);
  57. assertEquals(
  58. 'Node 0 cell should be the container\'s active descendant',
  59. palette.getRenderer().getCellForItem(nodes[0]),
  60. goog.a11y.aria.getActiveDescendant(container.getElement()));
  61. palette.setHighlightedItem(nodes[1]);
  62. assertEquals(
  63. 'Node 1 cell should be the container\'s active descendant',
  64. palette.getRenderer().getCellForItem(nodes[1]),
  65. goog.a11y.aria.getActiveDescendant(container.getElement()));
  66. palette.setHighlightedItem();
  67. assertNull(
  68. 'Container should have no active descendant',
  69. goog.a11y.aria.getActiveDescendant(container.getElement()));
  70. container.dispose();
  71. }
  72. function testHighlightCellEvents() {
  73. var container = new goog.ui.Container();
  74. container.render(document.getElementById('sandbox'));
  75. container.addChild(palette, true);
  76. var renderer = palette.getRenderer();
  77. var events = [];
  78. var targetElements = [];
  79. var handleEvent = function(e) {
  80. events.push(e);
  81. targetElements.push(e.target.getElement());
  82. };
  83. palette.getHandler().listen(
  84. palette,
  85. [
  86. this, goog.ui.Component.EventType.HIGHLIGHT, this,
  87. goog.ui.Component.EventType.UNHIGHLIGHT
  88. ],
  89. handleEvent);
  90. // Test highlight events on first selection
  91. palette.setHighlightedItem(nodes[0]);
  92. assertEquals('Should have fired 1 event', 1, events.length);
  93. assertEquals(
  94. 'HIGHLIGHT event should be fired', goog.ui.Component.EventType.HIGHLIGHT,
  95. events[0].type);
  96. assertEquals(
  97. 'Event should be fired for node[0] cell',
  98. renderer.getCellForItem(nodes[0]), targetElements[0]);
  99. events = [];
  100. targetElements = [];
  101. // Only fire highlight events when there is a selection change
  102. palette.setHighlightedItem(nodes[0]);
  103. assertEquals('Should have fired 0 events', 0, events.length);
  104. // Test highlight events on cell change
  105. palette.setHighlightedItem(nodes[1]);
  106. assertEquals('Should have fired 2 events', 2, events.length);
  107. var unhighlightEvent = events.shift();
  108. var highlightEvent = events.shift();
  109. assertEquals(
  110. 'UNHIGHLIGHT should be fired first',
  111. goog.ui.Component.EventType.UNHIGHLIGHT, unhighlightEvent.type);
  112. assertEquals(
  113. 'UNHIGHLIGHT should be fired for node[0] cell',
  114. renderer.getCellForItem(nodes[0]), targetElements[0]);
  115. assertEquals(
  116. 'HIGHLIGHT should be fired after UNHIGHLIGHT',
  117. goog.ui.Component.EventType.HIGHLIGHT, highlightEvent.type);
  118. assertEquals(
  119. 'HIGHLIGHT should be fired for node[1] cell',
  120. renderer.getCellForItem(nodes[1]), targetElements[1]);
  121. events = [];
  122. targetElements = [];
  123. // Test highlight events when a cell is unselected
  124. palette.setHighlightedItem();
  125. assertEquals('Should have fired 1 event', 1, events.length);
  126. assertEquals(
  127. 'UNHIGHLIGHT event should be fired',
  128. goog.ui.Component.EventType.UNHIGHLIGHT, events[0].type);
  129. assertEquals(
  130. 'Event should be fired for node[1] cell',
  131. renderer.getCellForItem(nodes[1]), targetElements[0]);
  132. }
  133. function testHandleKeyEventLoops() {
  134. palette.setHighlightedIndex(0);
  135. var createKeyEvent = function(keyCode) {
  136. return new goog.events.KeyEvent(
  137. keyCode, 0 /* charCode */, false /* repeat */,
  138. new goog.testing.events.Event(goog.events.EventType.KEYDOWN));
  139. };
  140. palette.handleKeyEvent(createKeyEvent(goog.events.KeyCodes.LEFT));
  141. assertEquals(nodes.length - 1, palette.getHighlightedIndex());
  142. palette.handleKeyEvent(createKeyEvent(goog.events.KeyCodes.RIGHT));
  143. assertEquals(0, palette.getHighlightedIndex());
  144. }
  145. function testSetHighlight() {
  146. assertEquals(-1, palette.getHighlightedIndex());
  147. palette.setHighlighted(true);
  148. assertEquals(0, palette.getHighlightedIndex());
  149. palette.setHighlightedIndex(3);
  150. palette.setHighlighted(false);
  151. assertEquals(-1, palette.getHighlightedIndex());
  152. palette.setHighlighted(true);
  153. assertEquals(3, palette.getHighlightedIndex());
  154. palette.setHighlighted(false);
  155. palette.setHighlightedIndex(5);
  156. palette.setHighlighted(true);
  157. assertEquals(5, palette.getHighlightedIndex());
  158. palette.setHighlighted(true);
  159. assertEquals(5, palette.getHighlightedIndex());
  160. }
  161. function testPerformActionInternal() {
  162. var container = new goog.ui.Container();
  163. container.render(document.getElementById('sandbox'));
  164. container.addChild(palette, true);
  165. palette.setActive(true);
  166. palette.setSelectedIndex(1);
  167. palette.setHighlightedIndex(3);
  168. palette.setHighlighted(true);
  169. assertEquals(1, palette.getSelectedIndex());
  170. assertEquals(3, palette.getHighlightedIndex());
  171. // Click somewhere in the palette, but not inside a cell.
  172. var mouseUp = new goog.events.BrowserEvent(
  173. {type: 'mouseup', button: 1, target: palette});
  174. palette.handleMouseUp(mouseUp);
  175. // Highlight and selection are both unchanged (user did not select anything).
  176. assertEquals(1, palette.getSelectedIndex());
  177. assertEquals(3, palette.getHighlightedIndex());
  178. }
  179. function testSetAriaLabel() {
  180. assertNull(
  181. 'Palette must not have aria label by default', palette.getAriaLabel());
  182. palette.setAriaLabel('My Palette');
  183. palette.render();
  184. var element = palette.getElementStrict();
  185. assertNotNull('Element must not be null', element);
  186. assertEquals(
  187. 'Palette element must have expected aria-label', 'My Palette',
  188. element.getAttribute('aria-label'));
  189. assertEquals(
  190. 'Palette element must have expected aria role', 'grid',
  191. element.getAttribute('role'));
  192. palette.setAriaLabel('My new Palette');
  193. assertEquals(
  194. 'Palette element must have updated aria-label', 'My new Palette',
  195. element.getAttribute('aria-label'));
  196. }