dimensionpicker_test.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.DimensionPickerTest');
  15. goog.setTestOnly('goog.ui.DimensionPickerTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.events.KeyCodes');
  19. goog.require('goog.math.Size');
  20. goog.require('goog.testing.jsunit');
  21. goog.require('goog.testing.ui.rendererasserts');
  22. goog.require('goog.ui.DimensionPicker');
  23. goog.require('goog.ui.DimensionPickerRenderer');
  24. var picker;
  25. var render;
  26. var decorate;
  27. function setUpPage() {
  28. render = goog.dom.getElement('render');
  29. decorate = goog.dom.getElement('decorate');
  30. }
  31. function setUp() {
  32. picker = new goog.ui.DimensionPicker();
  33. goog.dom.removeChildren(render);
  34. goog.dom.removeChildren(decorate);
  35. }
  36. function tearDown() {
  37. picker.dispose();
  38. }
  39. function testConstructor() {
  40. assertNotNull('Should have successful construction', picker);
  41. assertNull('Should not be in document', picker.getElement());
  42. }
  43. function testRender() {
  44. picker.render(render);
  45. assertEquals('Should create 1 child', 1, render.childNodes.length);
  46. assertEquals(
  47. 'Should be a div', String(goog.dom.TagName.DIV), render.firstChild.tagName);
  48. }
  49. function testDecorate() {
  50. picker.decorate(decorate);
  51. assertNotEquals(
  52. 'Should add several children', decorate.firstChild, decorate.lastChild);
  53. }
  54. function testHighlightedSize() {
  55. picker.render(render);
  56. var size = picker.getValue();
  57. assertEquals('Should have 1 column highlighted initially.', 1, size.width);
  58. assertEquals('Should have 1 row highlighted initially.', 1, size.height);
  59. picker.setValue(1, 2);
  60. size = picker.getValue();
  61. assertEquals('Should have 1 column highlighted.', 1, size.width);
  62. assertEquals('Should have 2 rows highlighted.', 2, size.height);
  63. picker.setValue(new goog.math.Size(3, 4));
  64. size = picker.getValue();
  65. assertEquals('Should have 3 columns highlighted.', 3, size.width);
  66. assertEquals('Should have 4 rows highlighted.', 4, size.height);
  67. picker.setValue(new goog.math.Size(-3, 0));
  68. size = picker.getValue();
  69. assertEquals(
  70. 'Should have 1 column highlighted when passed a negative ' +
  71. 'column value.',
  72. 1, size.width);
  73. assertEquals(
  74. 'Should have 1 row highlighted when passed 0 as the row ' +
  75. 'value.',
  76. 1, size.height);
  77. picker.setValue(picker.maxColumns + 10, picker.maxRows + 2);
  78. size = picker.getValue();
  79. assertEquals(
  80. 'Column value should be decreased to match max columns ' +
  81. 'if it is too high.',
  82. picker.maxColumns, size.width);
  83. assertEquals(
  84. 'Row value should be decreased to match max rows ' +
  85. 'if it is too high.',
  86. picker.maxRows, size.height);
  87. }
  88. function testSizeShown() {
  89. picker.render(render);
  90. var size = picker.getSize();
  91. assertEquals('Should have 5 columns visible', 5, size.width);
  92. assertEquals('Should have 5 rows visible', 5, size.height);
  93. picker.setValue(4, 4);
  94. size = picker.getSize();
  95. assertEquals('Should have 5 columns visible', 5, size.width);
  96. assertEquals('Should have 5 rows visible', 5, size.height);
  97. picker.setValue(12, 13);
  98. size = picker.getSize();
  99. assertEquals('Should have 13 columns visible', 13, size.width);
  100. assertEquals('Should have 14 rows visible', 14, size.height);
  101. picker.setValue(20, 20);
  102. size = picker.getSize();
  103. assertEquals('Should have 20 columns visible', 20, size.width);
  104. assertEquals('Should have 20 rows visible', 20, size.height);
  105. picker.setValue(2, 3);
  106. size = picker.getSize();
  107. assertEquals('Should have 5 columns visible', 5, size.width);
  108. assertEquals('Should have 5 rows visible', 5, size.height);
  109. }
  110. function testHandleMove() {
  111. picker.render(render);
  112. var renderer = picker.getRenderer();
  113. var mouseMoveElem = renderer.getMouseMoveElement(picker);
  114. picker.rightToLeft_ = false;
  115. var e = {
  116. target: mouseMoveElem,
  117. offsetX: 18, // Each grid square currently a magic 18px.
  118. offsetY: 36
  119. };
  120. picker.handleMouseMove(e);
  121. var size = picker.getValue();
  122. assertEquals('Should have 1 column highlighted', 1, size.width);
  123. assertEquals('Should have 2 rows highlighted', 2, size.height);
  124. picker.rightToLeft_ = true;
  125. picker.handleMouseMove(e);
  126. var size = picker.getValue();
  127. // In RTL we pick from the right side of the picker, so an offsetX of 0
  128. // would actually mean select all columns.
  129. assertEquals(
  130. 'Should have columns to the right of the mouse highlighted',
  131. Math.ceil((mouseMoveElem.offsetWidth - e.offsetX) / 18), size.width);
  132. assertEquals('Should have 2 rows highlighted', 2, size.height);
  133. }
  134. function testHandleKeyboardEvents() {
  135. picker.render(render);
  136. picker.rightToLeft_ = false;
  137. var result = picker.handleKeyEvent({keyCode: goog.events.KeyCodes.DOWN});
  138. var size = picker.getValue();
  139. assertEquals('Should have 1 column highlighted', 1, size.width);
  140. assertEquals('Should have 2 rows highlighted', 2, size.height);
  141. assertTrue('Should handle DOWN key event', result);
  142. result = picker.handleKeyEvent({keyCode: goog.events.KeyCodes.RIGHT});
  143. size = picker.getValue();
  144. assertEquals('Should have 2 column highlighted', 2, size.width);
  145. assertEquals('Should have 2 rows highlighted', 2, size.height);
  146. assertTrue('Should handle RIGHT key event', result);
  147. result = picker.handleKeyEvent({keyCode: goog.events.KeyCodes.UP});
  148. size = picker.getValue();
  149. assertEquals('Should have 2 column highlighted', 2, size.width);
  150. assertEquals('Should have 1 rows highlighted', 1, size.height);
  151. assertTrue('Should handle UP key event', result);
  152. // Pressing UP when there is only 1 row should be handled but has no
  153. // effect.
  154. result = picker.handleKeyEvent({keyCode: goog.events.KeyCodes.UP});
  155. size = picker.getValue();
  156. assertEquals('Should have 2 column highlighted', 2, size.width);
  157. assertEquals('Should have 1 rows highlighted', 1, size.height);
  158. assertTrue('Should handle UP key event', result);
  159. result = picker.handleKeyEvent({keyCode: goog.events.KeyCodes.LEFT});
  160. size = picker.getValue();
  161. assertEquals('Should have 2 column highlighted', 1, size.width);
  162. assertEquals('Should have 1 rows highlighted', 1, size.height);
  163. assertTrue('Should handle LEFT key event', result);
  164. // Pressing LEFT when there is only 1 row should not be handled
  165. // allowing SubMenu to close.
  166. result = picker.handleKeyEvent({keyCode: goog.events.KeyCodes.LEFT});
  167. assertFalse(
  168. 'Should not handle LEFT key event when there is only one column', result);
  169. picker.rightToLeft_ = true;
  170. // In RTL the roles of the LEFT and RIGHT keys are swapped.
  171. result = picker.handleKeyEvent({keyCode: goog.events.KeyCodes.LEFT});
  172. size = picker.getValue();
  173. assertEquals('Should have 2 column highlighted', 2, size.width);
  174. assertEquals('Should have 2 rows highlighted', 1, size.height);
  175. assertTrue('Should handle LEFT key event', result);
  176. result = picker.handleKeyEvent({keyCode: goog.events.KeyCodes.RIGHT});
  177. size = picker.getValue();
  178. assertEquals('Should have 2 column highlighted', 1, size.width);
  179. assertEquals('Should have 1 rows highlighted', 1, size.height);
  180. assertTrue('Should handle RIGHT key event', result);
  181. result = picker.handleKeyEvent({keyCode: goog.events.KeyCodes.RIGHT});
  182. assertFalse(
  183. 'Should not handle RIGHT key event when there is only one column',
  184. result);
  185. }
  186. function testDispose() {
  187. var element = picker.getElement();
  188. picker.render(render);
  189. picker.dispose();
  190. assertTrue('Picker should have been disposed of', picker.isDisposed());
  191. assertNull(
  192. 'Picker element reference should have been nulled out',
  193. picker.getElement());
  194. }
  195. function testRendererDoesntCallGetCssClassInConstructor() {
  196. goog.testing.ui.rendererasserts.assertNoGetCssClassCallsInConstructor(
  197. goog.ui.DimensionPickerRenderer);
  198. }
  199. function testSetAriaLabel() {
  200. assertNull(
  201. 'Picker must not have aria label by default', picker.getAriaLabel());
  202. picker.setAriaLabel('My picker');
  203. picker.render(render);
  204. var element = picker.getElementStrict();
  205. assertNotNull('Element must not be null', element);
  206. assertEquals(
  207. 'Picker element must have expected aria-label', 'My picker',
  208. element.getAttribute('aria-label'));
  209. assertTrue(goog.dom.isFocusableTabIndex(element));
  210. picker.setAriaLabel('My new picker');
  211. assertEquals(
  212. 'Picker element must have updated aria-label', 'My new picker',
  213. element.getAttribute('aria-label'));
  214. }