labelinput_test.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Copyright 2011 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.LabelInputTest');
  15. goog.setTestOnly('goog.ui.LabelInputTest');
  16. goog.require('goog.a11y.aria');
  17. goog.require('goog.a11y.aria.State');
  18. goog.require('goog.dom');
  19. goog.require('goog.dom.classlist');
  20. goog.require('goog.events.EventType');
  21. goog.require('goog.testing.MockClock');
  22. goog.require('goog.testing.events');
  23. goog.require('goog.testing.events.Event');
  24. goog.require('goog.testing.jsunit');
  25. goog.require('goog.ui.LabelInput');
  26. goog.require('goog.userAgent');
  27. var labelInput;
  28. var mockClock;
  29. function setUp() {
  30. mockClock = new goog.testing.MockClock(true);
  31. }
  32. function tearDown() {
  33. mockClock.dispose();
  34. labelInput.dispose();
  35. }
  36. function testGetLabel() {
  37. labelInput = new goog.ui.LabelInput();
  38. assertEquals('no label', '', labelInput.getLabel());
  39. labelInput = new goog.ui.LabelInput('search');
  40. assertEquals('label is given in the ctor', 'search', labelInput.getLabel());
  41. }
  42. function testSetLabel() {
  43. labelInput = new goog.ui.LabelInput();
  44. labelInput.setLabel('search');
  45. assertEquals('label is set', 'search', labelInput.getLabel());
  46. labelInput.createDom();
  47. labelInput.enterDocument();
  48. mockClock.tick(10);
  49. assertNotNull(labelInput.getElement());
  50. assertLabelValue(labelInput, 'search');
  51. labelInput.setLabel('new label');
  52. assertLabelValue(labelInput, 'new label');
  53. }
  54. function assertLabelValue(labelInput, expectedLabel) {
  55. assertEquals(
  56. 'label should have aria-label attribute \'' + expectedLabel + '\'',
  57. expectedLabel, goog.a11y.aria.getState(
  58. labelInput.getElement(), goog.a11y.aria.State.LABEL));
  59. // When browsers support the placeholder attribute, we use that instead of
  60. // the value property - and this test will fail.
  61. if (!isPlaceholderSupported()) {
  62. assertEquals(
  63. 'label is updated', expectedLabel, labelInput.getElement().value);
  64. } else {
  65. assertEquals('value is empty', '', labelInput.getElement().value);
  66. }
  67. }
  68. function testPlaceholderAttribute() {
  69. labelInput = new goog.ui.LabelInput();
  70. labelInput.setLabel('search');
  71. // Some browsers don't support the placeholder attribute, in which case we
  72. // this test will fail.
  73. if (!isPlaceholderSupported()) {
  74. return;
  75. }
  76. labelInput.createDom();
  77. labelInput.enterDocument();
  78. mockClock.tick(10);
  79. assertEquals(
  80. 'label should have placeholder attribute \'search\'', 'search',
  81. labelInput.getElement().placeholder);
  82. labelInput.setLabel('new label');
  83. assertEquals(
  84. 'label should have aria-label attribute \'new label\'', 'new label',
  85. labelInput.getElement().placeholder);
  86. }
  87. function testDecorateElementWithExistingPlaceholderAttribute() {
  88. labelInput = new goog.ui.LabelInput();
  89. labelInput.setLabel('search');
  90. labelInput.decorate(goog.dom.getElement('p'));
  91. labelInput.enterDocument();
  92. mockClock.tick(10);
  93. // The presence of an existing placeholder doesn't necessarily mean the
  94. // browser supports placeholders. Make sure labels are used for browsers
  95. // without placeholder support:
  96. if (isPlaceholderSupported()) {
  97. assertEquals(
  98. 'label should have placeholder attribute \'search\'', 'search',
  99. labelInput.getElement().placeholder);
  100. } else {
  101. assertNotNull(labelInput.getElement());
  102. assertEquals('label is rendered', 'search', labelInput.getElement().value);
  103. assertEquals(
  104. 'label should have aria-label attribute \'search\'', 'search',
  105. goog.a11y.aria.getState(
  106. labelInput.getElement(), goog.a11y.aria.State.LABEL));
  107. }
  108. }
  109. function testDecorateElementWithFocus() {
  110. labelInput = new goog.ui.LabelInput();
  111. labelInput.setLabel('search');
  112. var decoratedElement = goog.dom.getElement('i');
  113. decoratedElement.value = '';
  114. decoratedElement.focus();
  115. labelInput.decorate(decoratedElement);
  116. labelInput.enterDocument();
  117. mockClock.tick(10);
  118. assertEquals(
  119. 'label for pre-focused input should not have LABEL_CLASS_NAME', -1,
  120. labelInput.getElement().className.indexOf(labelInput.labelCssClassName));
  121. if (!isPlaceholderSupported()) {
  122. assertEquals(
  123. 'label rendered for pre-focused element', '',
  124. labelInput.getElement().value);
  125. // NOTE(user): element.blur() doesn't seem to trigger the BLUR event in
  126. // IE in the test environment. This could be related to the IE issues with
  127. // testClassName() below.
  128. goog.testing.events.fireBrowserEvent(
  129. new goog.testing.events.Event(
  130. goog.events.EventType.BLUR, decoratedElement));
  131. mockClock.tick(10);
  132. assertEquals(
  133. 'label not rendered for blurred element', 'search',
  134. labelInput.getElement().value);
  135. }
  136. }
  137. function testDecorateElementWithFocusDelay() {
  138. if (isPlaceholderSupported()) {
  139. return; // Delay only affects the older browsers.
  140. }
  141. var placeholder = 'search';
  142. labelInput = new goog.ui.LabelInput();
  143. labelInput.setLabel(placeholder);
  144. var delay = 150;
  145. labelInput.labelRestoreDelayMs = delay;
  146. var decoratedElement = goog.dom.getElement('i');
  147. decoratedElement.value = '';
  148. decoratedElement.focus();
  149. labelInput.decorate(decoratedElement);
  150. labelInput.enterDocument();
  151. // wait for all initial setup to settle
  152. mockClock.tick(delay);
  153. // NOTE(user): element.blur() doesn't seem to trigger the BLUR event in
  154. // IE in the test environment. This could be related to the IE issues with
  155. // testClassName() below.
  156. goog.testing.events.fireBrowserEvent(
  157. new goog.testing.events.Event(
  158. goog.events.EventType.BLUR, decoratedElement));
  159. mockClock.tick(delay - 1);
  160. assertEquals(
  161. 'label should not be restored before labelRestoreDelay', '',
  162. labelInput.getElement().value);
  163. mockClock.tick(1);
  164. assertEquals(
  165. 'label not rendered for blurred element with labelRestoreDelay',
  166. placeholder, labelInput.getElement().value);
  167. }
  168. function testClassName() {
  169. labelInput = new goog.ui.LabelInput();
  170. // EDGE/IE always fails this test, suspect it is a focus issue.
  171. if (goog.userAgent.EDGE_OR_IE) {
  172. return;
  173. }
  174. // FF does not perform focus if the window is not active in the first place.
  175. if (goog.userAgent.GECKO && document.hasFocus && !document.hasFocus()) {
  176. return;
  177. }
  178. labelInput.decorate(goog.dom.getElement('i'));
  179. labelInput.setLabel('search');
  180. var el = labelInput.getElement();
  181. assertTrue(
  182. 'label before focus should have LABEL_CLASS_NAME',
  183. goog.dom.classlist.contains(el, labelInput.labelCssClassName));
  184. labelInput.getElement().focus();
  185. assertFalse(
  186. 'label after focus should not have LABEL_CLASS_NAME',
  187. goog.dom.classlist.contains(el, labelInput.labelCssClassName));
  188. labelInput.getElement().blur();
  189. assertTrue(
  190. 'label after blur should have LABEL_CLASS_NAME',
  191. goog.dom.classlist.contains(el, labelInput.labelCssClassName));
  192. }
  193. function isPlaceholderSupported() {
  194. if (goog.dom.getElement('i').placeholder != null) {
  195. return true;
  196. }
  197. }
  198. function testEnable() {
  199. labelInput = new goog.ui.LabelInput();
  200. labelInput.createDom();
  201. labelInput.enterDocument();
  202. var labelElement = labelInput.getElement();
  203. var disabledClass = goog.getCssName(labelInput.labelCssClassName, 'disabled');
  204. assertTrue('label should be enabled', labelInput.isEnabled());
  205. assertFalse(
  206. 'label should not have the disabled class',
  207. goog.dom.classlist.contains(labelElement, disabledClass));
  208. labelInput.setEnabled(false);
  209. assertFalse('label should be disabled', labelInput.isEnabled());
  210. assertTrue(
  211. 'label should have the disabled class',
  212. goog.dom.classlist.contains(labelElement, disabledClass));
  213. labelInput.setEnabled(true);
  214. assertTrue('label should be enabled', labelInput.isEnabled());
  215. assertFalse(
  216. 'label should not have the disabled class',
  217. goog.dom.classlist.contains(labelElement, disabledClass));
  218. }