menuitemrenderer_test.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.MenuItemRendererTest');
  15. goog.setTestOnly('goog.ui.MenuItemRendererTest');
  16. goog.require('goog.a11y.aria');
  17. goog.require('goog.a11y.aria.Role');
  18. goog.require('goog.a11y.aria.State');
  19. goog.require('goog.dom');
  20. goog.require('goog.dom.classlist');
  21. goog.require('goog.testing.jsunit');
  22. goog.require('goog.testing.ui.rendererasserts');
  23. goog.require('goog.ui.Component');
  24. goog.require('goog.ui.MenuItem');
  25. goog.require('goog.ui.MenuItemRenderer');
  26. var sandbox;
  27. var item, renderer;
  28. function setUp() {
  29. sandbox = goog.dom.getElement('sandbox');
  30. item = new goog.ui.MenuItem('Hello');
  31. renderer = goog.ui.MenuItemRenderer.getInstance();
  32. }
  33. function tearDown() {
  34. item.dispose();
  35. goog.dom.removeChildren(sandbox);
  36. }
  37. function testMenuItemRenderer() {
  38. assertNotNull('Instance must not be null', renderer);
  39. assertEquals(
  40. 'Singleton getter must always return same instance', renderer,
  41. goog.ui.MenuItemRenderer.getInstance());
  42. }
  43. function testCreateDom() {
  44. var element = renderer.createDom(item);
  45. assertNotNull('Element must not be null', element);
  46. assertSameElements(
  47. 'Element must have the expected class names', ['goog-menuitem'],
  48. goog.dom.classlist.get(element));
  49. assertEquals(
  50. 'Element must have exactly one child element', 1,
  51. element.childNodes.length);
  52. assertHTMLEquals(
  53. 'Child element must have the expected structure',
  54. '<div class="goog-menuitem-content">Hello</div>', element.innerHTML);
  55. }
  56. function testCreateDomWithHoverState() {
  57. item.setHighlighted(true);
  58. var element = renderer.createDom(item);
  59. assertSameElements(
  60. 'Element must have the expected class names',
  61. ['goog-menuitem', 'goog-menuitem-highlight'],
  62. goog.dom.classlist.get(element));
  63. }
  64. function testCreateDomForCheckableItem() {
  65. item.setSupportedState(goog.ui.Component.State.CHECKED, true);
  66. item.render();
  67. var element = item.getElement();
  68. assertNotNull(element);
  69. assertSameElements(
  70. 'Element must have the expected class names',
  71. ['goog-menuitem', 'goog-option'], goog.dom.classlist.get(element));
  72. assertEquals(
  73. 'Element must have ARIA role menuitemcheckbox',
  74. goog.a11y.aria.Role.MENU_ITEM_CHECKBOX, goog.a11y.aria.getRole(element));
  75. item.setChecked(true);
  76. assertTrue('Item must be checked', item.isChecked());
  77. assertSameElements(
  78. 'Checked item must have the expected class names',
  79. ['goog-menuitem', 'goog-option', 'goog-option-selected'],
  80. goog.dom.classlist.get(element));
  81. assertEquals(
  82. 'Item must have checked ARIA state', 'true',
  83. goog.a11y.aria.getState(element, goog.a11y.aria.State.CHECKED));
  84. }
  85. function testCreateUpdateDomForCheckableItem() {
  86. // Render the item first, then update its supported states to include CHECKED.
  87. item.render();
  88. var element = item.getElement();
  89. item.setSupportedState(goog.ui.Component.State.CHECKED, true);
  90. assertNotNull(element);
  91. assertSameElements(
  92. 'Element must have the expected class names',
  93. ['goog-menuitem', 'goog-option'], goog.dom.classlist.get(element));
  94. assertEquals(
  95. 'Element must have ARIA role menuitemcheckbox',
  96. goog.a11y.aria.Role.MENU_ITEM_CHECKBOX, goog.a11y.aria.getRole(element));
  97. // Now actually check the item.
  98. item.setChecked(true);
  99. assertTrue('Item must be checked', item.isChecked());
  100. assertSameElements(
  101. 'Checked item must have the expected class names',
  102. ['goog-menuitem', 'goog-option', 'goog-option-selected'],
  103. goog.dom.classlist.get(element));
  104. assertEquals(
  105. 'Item must have checked ARIA state', 'true',
  106. goog.a11y.aria.getState(element, goog.a11y.aria.State.CHECKED));
  107. }
  108. function testCreateDomForSelectableItem() {
  109. item.setSupportedState(goog.ui.Component.State.SELECTED, true);
  110. item.render();
  111. var element = item.getElement();
  112. assertNotNull(element);
  113. assertSameElements(
  114. 'Element must have the expected class names',
  115. ['goog-menuitem', 'goog-option'], goog.dom.classlist.get(element));
  116. assertEquals(
  117. 'Element must have ARIA role menuitemradio',
  118. goog.a11y.aria.Role.MENU_ITEM_RADIO, goog.a11y.aria.getRole(element));
  119. item.setSelected(true);
  120. assertTrue('Item must be selected', item.isSelected());
  121. assertSameElements(
  122. 'Selected item must have the expected class names',
  123. ['goog-menuitem', 'goog-option', 'goog-option-selected'],
  124. goog.dom.classlist.get(element));
  125. assertEquals(
  126. 'Item must have selected ARIA state', 'true',
  127. goog.a11y.aria.getState(element, goog.a11y.aria.State.CHECKED));
  128. }
  129. function testCreateUpdateDomForSelectableItem() {
  130. // Render the item first, then update its supported states to include
  131. // SELECTED.
  132. item.render();
  133. var element = item.getElement();
  134. item.setSupportedState(goog.ui.Component.State.SELECTED, true);
  135. assertNotNull(element);
  136. assertSameElements(
  137. 'Element must have the expected class names',
  138. ['goog-menuitem', 'goog-option'], goog.dom.classlist.get(element));
  139. assertEquals(
  140. 'Element must have ARIA role menuitemradio',
  141. goog.a11y.aria.Role.MENU_ITEM_RADIO, goog.a11y.aria.getRole(element));
  142. // Now actually select the item.
  143. item.setSelected(true);
  144. assertTrue('Item must be selected', item.isSelected());
  145. assertSameElements(
  146. 'Selected item must have the expected class names',
  147. ['goog-menuitem', 'goog-option', 'goog-option-selected'],
  148. goog.dom.classlist.get(element));
  149. assertEquals(
  150. 'Item must have selected ARIA state', 'true',
  151. goog.a11y.aria.getState(element, goog.a11y.aria.State.CHECKED));
  152. }
  153. function testGetContentElement() {
  154. assertNull(
  155. 'Content element must be the null initially', item.getContentElement());
  156. item.createDom();
  157. assertEquals(
  158. 'Content element must be the element\'s first child',
  159. item.getElement().firstChild, item.getContentElement());
  160. }
  161. function testDecorate() {
  162. sandbox.innerHTML = '<div id="foo">Hello</div>';
  163. var foo = goog.dom.getElement('foo');
  164. var element = renderer.decorate(item, foo);
  165. assertSameElements(
  166. 'Element must have the expected class names', ['goog-menuitem'],
  167. goog.dom.classlist.get(element));
  168. assertEquals(
  169. 'Element must have exactly one child element', 1,
  170. element.childNodes.length);
  171. assertHTMLEquals(
  172. 'Child element must have the expected structure',
  173. '<div class="goog-menuitem-content">Hello</div>', element.innerHTML);
  174. }
  175. function testDecorateWithContentStructure() {
  176. sandbox.innerHTML =
  177. '<div id="foo"><div class="goog-menuitem-content">Hello</div></div>';
  178. var foo = goog.dom.getElement('foo');
  179. var element = renderer.decorate(item, foo);
  180. assertSameElements(
  181. 'Element must have the expected class names', ['goog-menuitem'],
  182. goog.dom.classlist.get(element));
  183. assertEquals(
  184. 'Element must have exactly one child element', 1,
  185. element.childNodes.length);
  186. assertHTMLEquals(
  187. 'Child element must have the expected structure',
  188. '<div class="goog-menuitem-content">Hello</div>', element.innerHTML);
  189. }
  190. function testDecorateWithHoverState() {
  191. sandbox.innerHTML =
  192. '<div id="foo" class="goog-menuitem-highlight">Hello</div>';
  193. var foo = goog.dom.getElement('foo');
  194. assertFalse('Item must not be highlighted', item.isHighlighted());
  195. var element = renderer.decorate(item, foo);
  196. assertSameElements(
  197. 'Element must have the expected class names',
  198. ['goog-menuitem', 'goog-menuitem-highlight'],
  199. goog.dom.classlist.get(element));
  200. assertTrue('Item must be highlighted', item.isHighlighted());
  201. }
  202. function testDecorateCheckableItem() {
  203. sandbox.innerHTML = '<div id="foo" class="goog-option">Hello</div>';
  204. var foo = goog.dom.getElement('foo');
  205. assertFalse(
  206. 'Item must not be checkable',
  207. item.isSupportedState(goog.ui.Component.State.CHECKED));
  208. var element = renderer.decorate(item, foo);
  209. assertSameElements(
  210. 'Element must have the expected class names',
  211. ['goog-menuitem', 'goog-option'], goog.dom.classlist.get(element));
  212. assertTrue(
  213. 'Item must be checkable',
  214. item.isSupportedState(goog.ui.Component.State.CHECKED));
  215. assertHTMLEquals(
  216. 'Child element must have the expected structure',
  217. '<div class="goog-menuitem-content">' +
  218. '<div class="goog-menuitem-checkbox"></div>Hello</div>',
  219. element.innerHTML);
  220. }
  221. function testSetContent() {
  222. item.setSupportedState(goog.ui.Component.State.CHECKED, true);
  223. var element = renderer.createDom(item);
  224. assertHTMLEquals(
  225. 'Child element must have the expected structure',
  226. '<div class="goog-menuitem-content">' +
  227. '<div class="goog-menuitem-checkbox"></div>Hello</div>',
  228. element.innerHTML);
  229. renderer.setContent(element, 'Goodbye');
  230. assertHTMLEquals(
  231. 'Child element must have the expected structure',
  232. '<div class="goog-menuitem-content">' +
  233. '<div class="goog-menuitem-checkbox"></div>Goodbye</div>',
  234. element.innerHTML);
  235. }
  236. function testDoesntCallGetCssClassInConstructor() {
  237. goog.testing.ui.rendererasserts.assertNoGetCssClassCallsInConstructor(
  238. goog.ui.MenuItemRenderer);
  239. }