nativebuttonrenderer_test.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.NativeButtonRendererTest');
  15. goog.setTestOnly('goog.ui.NativeButtonRendererTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.dom.classlist');
  19. goog.require('goog.events');
  20. goog.require('goog.testing.ExpectedFailures');
  21. goog.require('goog.testing.events');
  22. goog.require('goog.testing.jsunit');
  23. goog.require('goog.testing.ui.rendererasserts');
  24. goog.require('goog.ui.Button');
  25. goog.require('goog.ui.Component');
  26. goog.require('goog.ui.NativeButtonRenderer');
  27. goog.require('goog.userAgent');
  28. var sandbox;
  29. var renderer;
  30. var expectedFailures;
  31. var button;
  32. function setUpPage() {
  33. sandbox = goog.dom.getElement('sandbox');
  34. renderer = goog.ui.NativeButtonRenderer.getInstance();
  35. expectedFailures = new goog.testing.ExpectedFailures();
  36. }
  37. function setUp() {
  38. button = new goog.ui.Button('Hello', renderer);
  39. }
  40. function tearDown() {
  41. button.dispose();
  42. goog.dom.removeChildren(sandbox);
  43. expectedFailures.handleTearDown();
  44. }
  45. function testConstructor() {
  46. assertNotNull('Renderer must not be null', renderer);
  47. }
  48. function testGetAriaRole() {
  49. assertUndefined('ARIA role must be undefined', renderer.getAriaRole());
  50. }
  51. function testCreateDom() {
  52. button.setTooltip('Hello, world!');
  53. button.setValue('foo');
  54. var element = renderer.createDom(button);
  55. assertNotNull('Element must not be null', element);
  56. assertEquals('Element must be a button',
  57. String(goog.dom.TagName.BUTTON), element.tagName);
  58. assertSameElements(
  59. 'Button element must have expected class name', ['goog-button'],
  60. goog.dom.classlist.get(element));
  61. assertFalse('Button element must be enabled', element.disabled);
  62. assertEquals(
  63. 'Button element must have expected title', 'Hello, world!',
  64. element.title);
  65. // Expected to fail on IE.
  66. expectedFailures.expectFailureFor(goog.userAgent.IE);
  67. try {
  68. // See http://www.fourmilab.ch/fourmilog/archives/2007-03/000824.html
  69. // for a description of the problem.
  70. assertEquals(
  71. 'Button element must have expected value', 'foo', element.value);
  72. assertEquals(
  73. 'Button element must have expected contents', 'Hello',
  74. element.innerHTML);
  75. } catch (e) {
  76. expectedFailures.handleException(e);
  77. }
  78. assertFalse(
  79. 'Button must not handle its own mouse events',
  80. button.isHandleMouseEvents());
  81. assertFalse(
  82. 'Button must not support the custom FOCUSED state',
  83. button.isSupportedState(goog.ui.Component.State.FOCUSED));
  84. }
  85. function testCanDecorate() {
  86. sandbox.innerHTML = '<button id="buttonElement">Button</button>\n' +
  87. '<input id="inputButton" type="button" value="Input Button">\n' +
  88. '<input id="inputSubmit" type="submit" value="Input Submit">\n' +
  89. '<input id="inputReset" type="reset" value="Input Reset">\n' +
  90. '<input id="inputText" type="text" size="10">\n' +
  91. '<div id="divButton" class="goog-button">Hello</div>';
  92. assertTrue(
  93. 'Must be able to decorate <button>',
  94. renderer.canDecorate(goog.dom.getElement('buttonElement')));
  95. assertTrue(
  96. 'Must be able to decorate <input type="button">',
  97. renderer.canDecorate(goog.dom.getElement('inputButton')));
  98. assertTrue(
  99. 'Must be able to decorate <input type="submit">',
  100. renderer.canDecorate(goog.dom.getElement('inputSubmit')));
  101. assertTrue(
  102. 'Must be able to decorate <input type="reset">',
  103. renderer.canDecorate(goog.dom.getElement('inputReset')));
  104. assertFalse(
  105. 'Must not be able to decorate <input type="text">',
  106. renderer.canDecorate(goog.dom.getElement('inputText')));
  107. assertFalse(
  108. 'Must not be able to decorate <div class="goog-button">',
  109. renderer.canDecorate(goog.dom.getElement('divButton')));
  110. }
  111. function testDecorate() {
  112. sandbox.innerHTML =
  113. '<button id="foo" title="Hello!" value="bar">Foo Button</button>\n' +
  114. '<button id="disabledButton" value="bah" disabled="disabled">Disabled' +
  115. '</button>';
  116. var element = renderer.decorate(button, goog.dom.getElement('foo'));
  117. assertEquals(
  118. 'Decorated element must be as expected', goog.dom.getElement('foo'),
  119. element);
  120. assertEquals(
  121. 'Decorated button title must have expected value', 'Hello!',
  122. button.getTooltip());
  123. // Expected to fail on IE.
  124. expectedFailures.expectFailureFor(goog.userAgent.IE);
  125. try {
  126. // See http://www.fourmilab.ch/fourmilog/archives/2007-03/000824.html
  127. // for a description of the problem.
  128. assertEquals(
  129. 'Decorated button value must have expected value', 'bar',
  130. button.getValue());
  131. } catch (e) {
  132. expectedFailures.handleException(e);
  133. }
  134. assertFalse(
  135. 'Button must not handle its own mouse events',
  136. button.isHandleMouseEvents());
  137. assertFalse(
  138. 'Button must not support the custom FOCUSED state',
  139. button.isSupportedState(goog.ui.Component.State.FOCUSED));
  140. element = renderer.decorate(button, goog.dom.getElement('disabledButton'));
  141. assertFalse('Decorated button must be disabled', button.isEnabled());
  142. assertSameElements(
  143. 'Decorated button must have expected class names',
  144. ['goog-button', 'goog-button-disabled'], goog.dom.classlist.get(element));
  145. // Expected to fail on IE.
  146. expectedFailures.expectFailureFor(goog.userAgent.IE);
  147. try {
  148. // See http://www.fourmilab.ch/fourmilog/archives/2007-03/000824.html
  149. // for a description of the problem.
  150. assertEquals(
  151. 'Decorated button value must have expected value', 'bah',
  152. button.getValue());
  153. } catch (e) {
  154. expectedFailures.handleException(e);
  155. }
  156. assertFalse(
  157. 'Button must not handle its own mouse events',
  158. button.isHandleMouseEvents());
  159. assertFalse(
  160. 'Button must not support the custom FOCUSED state',
  161. button.isSupportedState(goog.ui.Component.State.FOCUSED));
  162. }
  163. function testInitializeDom() {
  164. var dispatchedActionCount = 0;
  165. var handleAction = function() { dispatchedActionCount++; };
  166. goog.events.listen(button, goog.ui.Component.EventType.ACTION, handleAction);
  167. button.render(sandbox);
  168. goog.testing.events.fireClickSequence(button.getElement());
  169. assertEquals(
  170. 'Button must have dispatched ACTION on click', 1, dispatchedActionCount);
  171. goog.events.unlisten(
  172. button, goog.ui.Component.EventType.ACTION, handleAction);
  173. }
  174. function testIsFocusable() {
  175. assertTrue('Enabled button must be focusable', renderer.isFocusable(button));
  176. button.setEnabled(false);
  177. assertFalse(
  178. 'Disabled button must not be focusable', renderer.isFocusable(button));
  179. }
  180. function testSetState() {
  181. button.render(sandbox);
  182. assertFalse(
  183. 'Button element must not be disabled', button.getElement().disabled);
  184. renderer.setState(button, goog.ui.Component.State.DISABLED, true);
  185. assertTrue('Button element must be disabled', button.getElement().disabled);
  186. }
  187. function testGetValue() {
  188. sandbox.innerHTML = '<button id="foo" value="blah">Hello</button>';
  189. // Expected to fail on IE.
  190. expectedFailures.expectFailureFor(goog.userAgent.IE);
  191. try {
  192. // See http://www.fourmilab.ch/fourmilog/archives/2007-03/000824.html
  193. // for a description of the problem.
  194. assertEquals(
  195. 'Value must be as expected', 'blah',
  196. renderer.getValue(goog.dom.getElement('foo')));
  197. } catch (e) {
  198. expectedFailures.handleException(e);
  199. }
  200. }
  201. function testSetValue() {
  202. button.render(sandbox);
  203. renderer.setValue(button.getElement(), 'What?');
  204. assertEquals(
  205. 'Button must have expected value', 'What?',
  206. renderer.getValue(button.getElement()));
  207. }
  208. function testDoesntCallGetCssClassInConstructor() {
  209. goog.testing.ui.rendererasserts.assertNoGetCssClassCallsInConstructor(
  210. goog.ui.NativeButtonRenderer);
  211. }