containerrenderer_test.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.ContainerRendererTest');
  15. goog.setTestOnly('goog.ui.ContainerRendererTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.style');
  19. goog.require('goog.testing.ExpectedFailures');
  20. goog.require('goog.testing.PropertyReplacer');
  21. goog.require('goog.testing.jsunit');
  22. goog.require('goog.testing.ui.rendererasserts');
  23. goog.require('goog.ui.Container');
  24. goog.require('goog.ui.ContainerRenderer');
  25. goog.require('goog.userAgent');
  26. var renderer;
  27. var expectedFailures;
  28. var stubs = new goog.testing.PropertyReplacer();
  29. function setUpPage() {
  30. expectedFailures = new goog.testing.ExpectedFailures();
  31. }
  32. function setUp() {
  33. var sandbox = goog.dom.getElement('sandbox');
  34. sandbox.appendChild(
  35. goog.dom.createDom(goog.dom.TagName.SPAN, {id: 'noTabIndex'}, 'Test'));
  36. sandbox.appendChild(
  37. goog.dom.createDom(
  38. goog.dom.TagName.DIV,
  39. {id: 'container', 'class': 'goog-container-horizontal'},
  40. goog.dom.createDom(
  41. goog.dom.TagName.DIV, {id: 'control', 'class': 'goog-control'},
  42. 'Hello, world!')));
  43. renderer = goog.ui.ContainerRenderer.getInstance();
  44. }
  45. function tearDown() {
  46. goog.dom.removeChildren(goog.dom.getElement('sandbox'));
  47. stubs.reset();
  48. expectedFailures.handleTearDown();
  49. }
  50. function testGetInstance() {
  51. assertTrue(
  52. 'getInstance() must return a ContainerRenderer',
  53. renderer instanceof goog.ui.ContainerRenderer);
  54. assertEquals(
  55. 'getInstance() must return the same object each time', renderer,
  56. goog.ui.ContainerRenderer.getInstance());
  57. }
  58. function testGetCustomRenderer() {
  59. var cssClass = 'special-css-class';
  60. var containerRenderer = goog.ui.ContainerRenderer.getCustomRenderer(
  61. goog.ui.ContainerRenderer, cssClass);
  62. assertEquals(
  63. 'Renderer should have returned the custom CSS class.', cssClass,
  64. containerRenderer.getCssClass());
  65. }
  66. function testGetAriaRole() {
  67. assertUndefined('ARIA role must be undefined', renderer.getAriaRole());
  68. }
  69. function testEnableTabIndex() {
  70. var container = goog.dom.getElement('container');
  71. assertFalse(
  72. 'Container must not have any tab index',
  73. goog.dom.isFocusableTabIndex(container));
  74. // WebKit on Mac doesn't support tabIndex for arbitrary DOM elements
  75. // until version 527 or later.
  76. expectedFailures.expectFailureFor(
  77. goog.userAgent.WEBKIT && goog.userAgent.MAC &&
  78. !goog.userAgent.isVersionOrHigher('527'));
  79. try {
  80. renderer.enableTabIndex(container, true);
  81. assertTrue(
  82. 'Container must have a tab index',
  83. goog.dom.isFocusableTabIndex(container));
  84. assertEquals('Container\'s tab index must be 0', 0, container.tabIndex);
  85. renderer.enableTabIndex(container, false);
  86. assertFalse(
  87. 'Container must not have a tab index',
  88. goog.dom.isFocusableTabIndex(container));
  89. assertEquals('Container\'s tab index must be -1', -1, container.tabIndex);
  90. } catch (e) {
  91. expectedFailures.handleException(e);
  92. }
  93. }
  94. function testCreateDom() {
  95. var horizontal =
  96. new goog.ui.Container(goog.ui.Container.Orientation.HORIZONTAL);
  97. var element1 = renderer.createDom(horizontal);
  98. assertEquals('Element must be a DIV', 'DIV', element1.tagName);
  99. assertEquals(
  100. 'Element must have the expected class name',
  101. 'goog-container goog-container-horizontal', element1.className);
  102. var vertical = new goog.ui.Container(goog.ui.Container.Orientation.VERTICAL);
  103. var element2 = renderer.createDom(vertical);
  104. assertEquals('Element must be a DIV', 'DIV', element2.tagName);
  105. assertEquals(
  106. 'Element must have the expected class name',
  107. 'goog-container goog-container-vertical', element2.className);
  108. }
  109. function testGetContentElement() {
  110. assertNull(
  111. 'getContentElement() must return null if element is null',
  112. renderer.getContentElement(null));
  113. var element = goog.dom.getElement('container');
  114. assertEquals(
  115. 'getContentElement() must return its argument', element,
  116. renderer.getContentElement(element));
  117. }
  118. function testCanDecorate() {
  119. assertFalse(
  120. 'canDecorate() must return false for a SPAN',
  121. renderer.canDecorate(goog.dom.getElement('noTabIndex')));
  122. assertTrue(
  123. 'canDecorate() must return true for a DIV',
  124. renderer.canDecorate(goog.dom.getElement('container')));
  125. }
  126. function testDecorate() {
  127. var container = new goog.ui.Container();
  128. var element = goog.dom.getElement('container');
  129. assertFalse(
  130. 'Container must not be in the document', container.isInDocument());
  131. container.decorate(element);
  132. assertTrue('Container must be in the document', container.isInDocument());
  133. assertEquals(
  134. 'Container\'s ID must match the decorated element\'s ID', element.id,
  135. container.getId());
  136. assertEquals(
  137. 'Element must have the expected class name',
  138. 'goog-container-horizontal goog-container', element.className);
  139. assertEquals('Container must have one child', 1, container.getChildCount());
  140. assertEquals(
  141. 'Child component\'s ID must be as expected', 'control',
  142. container.getChildAt(0).getId());
  143. assertThrows('Redecorating must throw error', function() {
  144. container.decorate(element);
  145. });
  146. }
  147. function testDecorateWithCustomContainerElement() {
  148. var element = goog.dom.getElement('container');
  149. var alternateContainerElement = goog.dom.createElement(goog.dom.TagName.DIV);
  150. element.appendChild(alternateContainerElement);
  151. var container = new goog.ui.Container();
  152. stubs.set(renderer, 'getContentElement', function() {
  153. return alternateContainerElement;
  154. });
  155. assertFalse(
  156. 'Container must not be in the document', container.isInDocument());
  157. container.decorate(element);
  158. assertTrue('Container must be in the document', container.isInDocument());
  159. assertEquals(
  160. 'Container\'s ID must match the decorated element\'s ID', element.id,
  161. container.getId());
  162. assertEquals(
  163. 'Element must have the expected class name',
  164. 'goog-container-horizontal goog-container', element.className);
  165. assertEquals('Container must have 0 children', 0, container.getChildCount());
  166. assertThrows('Redecorating must throw error', function() {
  167. container.decorate(element);
  168. });
  169. }
  170. function testSetStateFromClassName() {
  171. var container = new goog.ui.Container();
  172. assertEquals(
  173. 'Container must be vertical', goog.ui.Container.Orientation.VERTICAL,
  174. container.getOrientation());
  175. renderer.setStateFromClassName(
  176. container, 'goog-container-horizontal', 'goog-container');
  177. assertEquals(
  178. 'Container must be horizontal', goog.ui.Container.Orientation.HORIZONTAL,
  179. container.getOrientation());
  180. renderer.setStateFromClassName(
  181. container, 'goog-container-vertical', 'goog-container');
  182. assertEquals(
  183. 'Container must be vertical', goog.ui.Container.Orientation.VERTICAL,
  184. container.getOrientation());
  185. assertTrue('Container must be enabled', container.isEnabled());
  186. renderer.setStateFromClassName(
  187. container, 'goog-container-disabled', 'goog-container');
  188. assertFalse('Container must be disabled', container.isEnabled());
  189. }
  190. function testInitializeDom() {
  191. var container = new goog.ui.Container();
  192. var element = goog.dom.getElement('container');
  193. container.decorate(element);
  194. assertTrue(
  195. 'Container\'s root element must be unselectable',
  196. goog.style.isUnselectable(container.getElement()));
  197. assertEquals(
  198. 'On IE, container\'s root element must have hideFocus=true',
  199. goog.userAgent.IE, !!container.getElement().hideFocus);
  200. }
  201. function testDoesntCallGetCssClassInConstructor() {
  202. goog.testing.ui.rendererasserts.assertNoGetCssClassCallsInConstructor(
  203. goog.ui.ContainerRenderer);
  204. }