button_test.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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.ButtonTest');
  15. goog.setTestOnly('goog.ui.ButtonTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.classlist');
  18. goog.require('goog.events');
  19. goog.require('goog.events.Event');
  20. goog.require('goog.events.EventType');
  21. goog.require('goog.events.KeyCodes');
  22. goog.require('goog.events.KeyHandler');
  23. goog.require('goog.testing.events');
  24. goog.require('goog.testing.jsunit');
  25. goog.require('goog.ui.Button');
  26. goog.require('goog.ui.ButtonRenderer');
  27. goog.require('goog.ui.ButtonSide');
  28. goog.require('goog.ui.Component');
  29. goog.require('goog.ui.NativeButtonRenderer');
  30. var sandbox;
  31. var button;
  32. var clonedButtonDom;
  33. var demoButtonElement;
  34. function setUp() {
  35. sandbox = goog.dom.getElement('sandbox');
  36. button = new goog.ui.Button();
  37. demoButtonElement = goog.dom.getElement('demoButton');
  38. clonedButtonDom = demoButtonElement.cloneNode(true);
  39. }
  40. function tearDown() {
  41. button.dispose();
  42. demoButtonElement.parentNode.replaceChild(clonedButtonDom, demoButtonElement);
  43. goog.dom.removeChildren(sandbox);
  44. }
  45. function testConstructor() {
  46. assertNotNull('Button must not be null', button);
  47. assertEquals(
  48. 'Renderer must default to expected value',
  49. goog.ui.NativeButtonRenderer.getInstance(), button.getRenderer());
  50. var fakeDomHelper = {};
  51. var testButton = new goog.ui.Button(
  52. 'Hello', goog.ui.ButtonRenderer.getInstance(), fakeDomHelper);
  53. assertEquals(
  54. 'Content must have expected value', 'Hello', testButton.getContent());
  55. assertEquals(
  56. 'Renderer must have expected value', goog.ui.ButtonRenderer.getInstance(),
  57. testButton.getRenderer());
  58. assertEquals(
  59. 'DOM helper must have expected value', fakeDomHelper,
  60. testButton.getDomHelper());
  61. testButton.dispose();
  62. }
  63. function testGetSetValue() {
  64. assertUndefined(
  65. 'Button\'s value must default to undefined', button.getValue());
  66. button.setValue(17);
  67. assertEquals('Button must have expected value', 17, button.getValue());
  68. button.render(sandbox);
  69. assertEquals(
  70. 'Button element must have expected value', '17',
  71. button.getElement().value);
  72. button.setValue('foo');
  73. assertEquals(
  74. 'Button element must have updated value', 'foo',
  75. button.getElement().value);
  76. button.setValueInternal('bar');
  77. assertEquals('Button must have new internal value', 'bar', button.getValue());
  78. assertEquals(
  79. 'Button element must be unchanged', 'foo', button.getElement().value);
  80. }
  81. function testGetSetTooltip() {
  82. assertUndefined(
  83. 'Button\'s tooltip must default to undefined', button.getTooltip());
  84. button.setTooltip('Hello');
  85. assertEquals(
  86. 'Button must have expected tooltip', 'Hello', button.getTooltip());
  87. button.render(sandbox);
  88. assertEquals(
  89. 'Button element must have expected title', 'Hello',
  90. button.getElement().title);
  91. button.setTooltip('Goodbye');
  92. assertEquals(
  93. 'Button element must have updated title', 'Goodbye',
  94. button.getElement().title);
  95. button.setTooltipInternal('World');
  96. assertEquals(
  97. 'Button must have new internal tooltip', 'World', button.getTooltip());
  98. assertEquals(
  99. 'Button element must be unchanged', 'Goodbye', button.getElement().title);
  100. }
  101. function testSetCollapsed() {
  102. assertNull(
  103. 'Button must not have any collapsed styling by default',
  104. button.getExtraClassNames());
  105. button.setCollapsed(goog.ui.ButtonSide.START);
  106. assertSameElements(
  107. 'Button must have the start side collapsed',
  108. ['goog-button-collapse-left'], button.getExtraClassNames());
  109. button.render(sandbox);
  110. assertSameElements(
  111. 'Button element must have the start side collapsed',
  112. ['goog-button', 'goog-button-collapse-left'],
  113. goog.dom.classlist.get(button.getElement()));
  114. button.setCollapsed(goog.ui.ButtonSide.BOTH);
  115. assertSameElements(
  116. 'Button must have both sides collapsed',
  117. ['goog-button-collapse-left', 'goog-button-collapse-right'],
  118. button.getExtraClassNames());
  119. assertSameElements(
  120. 'Button element must have both sides collapsed',
  121. [
  122. 'goog-button', 'goog-button-collapse-left', 'goog-button-collapse-right'
  123. ],
  124. goog.dom.classlist.get(button.getElement()));
  125. }
  126. function testDispose() {
  127. assertFalse('Button must not have been disposed of', button.isDisposed());
  128. button.render(sandbox);
  129. button.setValue('foo');
  130. button.setTooltip('bar');
  131. button.dispose();
  132. assertTrue('Button must have been disposed of', button.isDisposed());
  133. assertUndefined('Button\'s value must have been deleted', button.getValue());
  134. assertUndefined(
  135. 'Button\'s tooltip must have been deleted', button.getTooltip());
  136. }
  137. function testBasicButtonBehavior() {
  138. var dispatchedActionCount = 0;
  139. var handleAction = function() { dispatchedActionCount++; };
  140. goog.events.listen(button, goog.ui.Component.EventType.ACTION, handleAction);
  141. button.decorate(demoButtonElement);
  142. goog.testing.events.fireClickSequence(demoButtonElement);
  143. assertEquals(
  144. 'Button must have dispatched ACTION on click', 1, dispatchedActionCount);
  145. dispatchedActionCount = 0;
  146. var e = new goog.events.Event(goog.events.KeyHandler.EventType.KEY, button);
  147. e.keyCode = goog.events.KeyCodes.ENTER;
  148. button.handleKeyEvent(e);
  149. assertEquals(
  150. 'Enabled button must have dispatched ACTION on Enter key', 1,
  151. dispatchedActionCount);
  152. dispatchedActionCount = 0;
  153. e = new goog.events.Event(goog.events.EventType.KEYUP, button);
  154. e.keyCode = goog.events.KeyCodes.SPACE;
  155. button.handleKeyEvent(e);
  156. assertEquals(
  157. 'Enabled button must have dispatched ACTION on Space key', 1,
  158. dispatchedActionCount);
  159. goog.events.unlisten(
  160. button, goog.ui.Component.EventType.ACTION, handleAction);
  161. }
  162. function testDisabledButtonBehavior() {
  163. var dispatchedActionCount = 0;
  164. var handleAction = function() { dispatchedActionCount++; };
  165. goog.events.listen(button, goog.ui.Component.EventType.ACTION, handleAction);
  166. button.setEnabled(false);
  167. dispatchedActionCount = 0;
  168. button.handleKeyEvent({keyCode: goog.events.KeyCodes.ENTER});
  169. assertEquals(
  170. 'Disabled button must not dispatch ACTION on Enter key', 0,
  171. dispatchedActionCount);
  172. dispatchedActionCount = 0;
  173. button.handleKeyEvent(
  174. {keyCode: goog.events.KeyCodes.SPACE, type: goog.events.EventType.KEYUP});
  175. assertEquals(
  176. 'Disabled button must not have dispatched ACTION on Space', 0,
  177. dispatchedActionCount);
  178. goog.events.unlisten(
  179. button, goog.ui.Component.EventType.ACTION, handleAction);
  180. }
  181. function testSpaceFireActionOnKeyUp() {
  182. var dispatchedActionCount = 0;
  183. var handleAction = function() { dispatchedActionCount++; };
  184. goog.events.listen(button, goog.ui.Component.EventType.ACTION, handleAction);
  185. dispatchedActionCount = 0;
  186. e = new goog.events.Event(goog.events.KeyHandler.EventType.KEY, button);
  187. e.keyCode = goog.events.KeyCodes.SPACE;
  188. button.handleKeyEvent(e);
  189. assertEquals(
  190. 'Button must not have dispatched ACTION on Space keypress', 0,
  191. dispatchedActionCount);
  192. assertEquals(
  193. 'The default action (scrolling) must have been prevented ' +
  194. 'for Space keypress',
  195. false, e.returnValue_);
  196. dispatchedActionCount = 0;
  197. e = new goog.events.Event(goog.events.EventType.KEYUP, button);
  198. e.keyCode = goog.events.KeyCodes.SPACE;
  199. button.handleKeyEvent(e);
  200. assertEquals(
  201. 'Button must have dispatched ACTION on Space keyup', 1,
  202. dispatchedActionCount);
  203. goog.events.unlisten(
  204. button, goog.ui.Component.EventType.ACTION, handleAction);
  205. }
  206. function testEnterFireActionOnKeyPress() {
  207. var dispatchedActionCount = 0;
  208. var handleAction = function() { dispatchedActionCount++; };
  209. goog.events.listen(button, goog.ui.Component.EventType.ACTION, handleAction);
  210. dispatchedActionCount = 0;
  211. e = new goog.events.Event(goog.events.KeyHandler.EventType.KEY, button);
  212. e.keyCode = goog.events.KeyCodes.ENTER;
  213. button.handleKeyEvent(e);
  214. assertEquals(
  215. 'Button must have dispatched ACTION on Enter keypress', 1,
  216. dispatchedActionCount);
  217. dispatchedActionCount = 0;
  218. e = new goog.events.Event(goog.events.EventType.KEYUP, button);
  219. e.keyCode = goog.events.KeyCodes.ENTER;
  220. button.handleKeyEvent(e);
  221. assertEquals(
  222. 'Button must not have dispatched ACTION on Enter keyup', 0,
  223. dispatchedActionCount);
  224. goog.events.unlisten(
  225. button, goog.ui.Component.EventType.ACTION, handleAction);
  226. }
  227. function testSetAriaLabel() {
  228. assertNull(
  229. 'Button must not have aria label by default', button.getAriaLabel());
  230. button.setAriaLabel('Button 1');
  231. button.render();
  232. assertEquals(
  233. 'Button element must have expected aria-label', 'Button 1',
  234. button.getElement().getAttribute('aria-label'));
  235. button.setAriaLabel('Button 2');
  236. assertEquals(
  237. 'Button element must have updated aria-label', 'Button 2',
  238. button.getElement().getAttribute('aria-label'));
  239. }
  240. function testSetAriaLabel_decorate() {
  241. assertNull(
  242. 'Button must not have aria label by default', button.getAriaLabel());
  243. button.setAriaLabel('Button 1');
  244. button.decorate(demoButtonElement);
  245. var el = button.getElementStrict();
  246. assertEquals(
  247. 'Button element must have expected aria-label', 'Button 1',
  248. el.getAttribute('aria-label'));
  249. assertEquals(
  250. 'Button element must have expected aria-role', 'button',
  251. el.getAttribute('role'));
  252. button.setAriaLabel('Button 2');
  253. assertEquals(
  254. 'Button element must have updated aria-label', 'Button 2',
  255. el.getAttribute('aria-label'));
  256. assertEquals(
  257. 'Button element must have expected aria-role', 'button',
  258. el.getAttribute('role'));
  259. }