splitpane_test.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright 2009 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.SplitPaneTest');
  15. goog.setTestOnly('goog.ui.SplitPaneTest');
  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.math.Size');
  21. goog.require('goog.style');
  22. goog.require('goog.testing.events');
  23. goog.require('goog.testing.jsunit');
  24. goog.require('goog.testing.recordFunction');
  25. goog.require('goog.ui.Component');
  26. goog.require('goog.ui.SplitPane');
  27. var splitpane;
  28. var leftComponent;
  29. var rightComponent;
  30. function setUp() {
  31. leftComponent = new goog.ui.Component();
  32. rightComponent = new goog.ui.Component();
  33. splitpane = new goog.ui.SplitPane(
  34. leftComponent, rightComponent, goog.ui.SplitPane.Orientation.HORIZONTAL);
  35. }
  36. function tearDown() {
  37. splitpane.dispose();
  38. leftComponent.dispose();
  39. rightComponent.dispose();
  40. goog.dom.removeChildren(goog.dom.getElement('sandbox'));
  41. }
  42. function testRender() {
  43. splitpane.render(goog.dom.getElement('sandbox'));
  44. assertEquals(
  45. 1,
  46. goog.dom
  47. .getElementsByTagNameAndClass(goog.dom.TagName.DIV, 'goog-splitpane')
  48. .length);
  49. assertEquals(
  50. 1, goog.dom
  51. .getElementsByTagNameAndClass(
  52. goog.dom.TagName.DIV, 'goog-splitpane-first-container')
  53. .length);
  54. assertEquals(
  55. 1, goog.dom
  56. .getElementsByTagNameAndClass(
  57. goog.dom.TagName.DIV, 'goog-splitpane-second-container')
  58. .length);
  59. assertEquals(
  60. 1, goog.dom
  61. .getElementsByTagNameAndClass(
  62. goog.dom.TagName.DIV, 'goog-splitpane-handle')
  63. .length);
  64. }
  65. function testDecorate() {
  66. var mainDiv = goog.dom.createDom(
  67. goog.dom.TagName.DIV, 'goog-splitpane',
  68. goog.dom.createDom(
  69. goog.dom.TagName.DIV, 'goog-splitpane-first-container'),
  70. goog.dom.createDom(
  71. goog.dom.TagName.DIV, 'goog-splitpane-second-container'),
  72. goog.dom.createDom(goog.dom.TagName.DIV, 'goog-splitpane-handle'));
  73. var sandbox = goog.dom.getElement('sandbox');
  74. goog.dom.appendChild(sandbox, mainDiv);
  75. splitpane.decorate(mainDiv);
  76. }
  77. function testDecorateWithNestedSplitPane() {
  78. // Create a standard split pane to be nested within another split pane.
  79. var innerSplitPaneDiv = goog.dom.createDom(
  80. goog.dom.TagName.DIV, 'goog-splitpane',
  81. goog.dom.createDom(
  82. goog.dom.TagName.DIV, 'goog-splitpane-first-container e1'),
  83. goog.dom.createDom(
  84. goog.dom.TagName.DIV, 'goog-splitpane-second-container e2'),
  85. goog.dom.createDom(goog.dom.TagName.DIV, 'goog-splitpane-handle e3'));
  86. // Create a split pane containing a split pane instance.
  87. var outerSplitPaneDiv = goog.dom.createDom(
  88. goog.dom.TagName.DIV, 'goog-splitpane',
  89. goog.dom.createDom(
  90. goog.dom.TagName.DIV, 'goog-splitpane-first-container e4',
  91. innerSplitPaneDiv),
  92. goog.dom.createDom(
  93. goog.dom.TagName.DIV, 'goog-splitpane-second-container e5'),
  94. goog.dom.createDom(goog.dom.TagName.DIV, 'goog-splitpane-handle e6'));
  95. var sandbox = goog.dom.getElement('sandbox');
  96. goog.dom.appendChild(sandbox, outerSplitPaneDiv);
  97. // Decorate and check that the correct containers and handle are used.
  98. splitpane.decorate(outerSplitPaneDiv);
  99. assertTrue(
  100. goog.dom.classlist.contains(splitpane.firstComponentContainer_, 'e4'));
  101. assertTrue(
  102. goog.dom.classlist.contains(splitpane.secondComponentContainer_, 'e5'));
  103. assertTrue(goog.dom.classlist.contains(splitpane.splitpaneHandle_, 'e6'));
  104. }
  105. function testSetSize() {
  106. splitpane.setInitialSize(200);
  107. splitpane.setHandleSize(10);
  108. splitpane.render(goog.dom.getElement('sandbox'));
  109. splitpane.setSize(new goog.math.Size(500, 300));
  110. assertEquals(200, splitpane.getFirstComponentSize());
  111. var splitpaneSize = goog.style.getBorderBoxSize(splitpane.getElement());
  112. assertEquals(500, splitpaneSize.width);
  113. assertEquals(300, splitpaneSize.height);
  114. }
  115. function testOrientationChange() {
  116. splitpane.setInitialSize(200);
  117. splitpane.setHandleSize(10);
  118. splitpane.render(goog.dom.getElement('sandbox'));
  119. splitpane.setSize(new goog.math.Size(500, 300));
  120. var first = goog.dom.getElementsByTagNameAndClass(
  121. goog.dom.TagName.DIV, 'goog-splitpane-first-container')[0];
  122. var second = goog.dom.getElementsByTagNameAndClass(
  123. goog.dom.TagName.DIV, 'goog-splitpane-second-container')[0];
  124. var handle = goog.dom.getElementsByTagNameAndClass(
  125. goog.dom.TagName.DIV, 'goog-splitpane-handle')[0];
  126. var handleSize = goog.style.getBorderBoxSize(handle);
  127. assertEquals(10, handleSize.width);
  128. assertEquals(300, handleSize.height);
  129. var firstSize = goog.style.getBorderBoxSize(first);
  130. assertEquals(200, firstSize.width);
  131. assertEquals(300, firstSize.height);
  132. var secondSize = goog.style.getBorderBoxSize(second);
  133. assertEquals(290, secondSize.width); // 500 - 200 - 10 = 290
  134. assertEquals(300, secondSize.height);
  135. splitpane.setOrientation(goog.ui.SplitPane.Orientation.VERTICAL);
  136. handleSize = goog.style.getBorderBoxSize(handle);
  137. assertEquals(10, handleSize.height);
  138. assertEquals(500, handleSize.width);
  139. firstSize = goog.style.getBorderBoxSize(first);
  140. assertEquals(120, firstSize.height); // 200 * 300/500 = 120
  141. assertEquals(500, firstSize.width);
  142. secondSize = goog.style.getBorderBoxSize(second);
  143. assertEquals(170, secondSize.height); // 300 - 120 - 10 = 170
  144. assertEquals(500, secondSize.width);
  145. splitpane.setOrientation(goog.ui.SplitPane.Orientation.HORIZONTAL);
  146. handleSize = goog.style.getBorderBoxSize(handle);
  147. assertEquals(10, handleSize.width);
  148. assertEquals(300, handleSize.height);
  149. firstSize = goog.style.getBorderBoxSize(first);
  150. assertEquals(200, firstSize.width);
  151. assertEquals(300, firstSize.height);
  152. secondSize = goog.style.getBorderBoxSize(second);
  153. assertEquals(290, secondSize.width);
  154. assertEquals(300, secondSize.height);
  155. }
  156. function testDragEvent() {
  157. splitpane.setInitialSize(200);
  158. splitpane.setHandleSize(10);
  159. splitpane.render(goog.dom.getElement('sandbox'));
  160. var handler = goog.testing.recordFunction();
  161. goog.events.listen(
  162. splitpane, goog.ui.SplitPane.EventType.HANDLE_DRAG, handler);
  163. var handle = goog.dom.getElementsByTagNameAndClass(
  164. goog.dom.TagName.DIV, 'goog-splitpane-handle')[0];
  165. goog.testing.events.fireMouseDownEvent(handle);
  166. goog.testing.events.fireMouseMoveEvent(handle);
  167. goog.testing.events.fireMouseUpEvent(handle);
  168. assertEquals('HANDLE_DRAG event expected', 1, handler.getCallCount());
  169. splitpane.setContinuousResize(false);
  170. handler.reset();
  171. goog.testing.events.fireMouseDownEvent(handle);
  172. goog.testing.events.fireMouseMoveEvent(handle);
  173. goog.testing.events.fireMouseUpEvent(handle);
  174. assertEquals('HANDLE_DRAG event not expected', 0, handler.getCallCount());
  175. }
  176. function testDragEndEvent() {
  177. splitpane.setInitialSize(200);
  178. splitpane.setHandleSize(10);
  179. splitpane.render(goog.dom.getElement('sandbox'));
  180. var handler = goog.testing.recordFunction();
  181. goog.events.listen(
  182. splitpane, goog.ui.SplitPane.EventType.HANDLE_DRAG_END, handler);
  183. var handle = goog.dom.getElementsByTagNameAndClass(
  184. goog.dom.TagName.DIV, 'goog-splitpane-handle')[0];
  185. goog.testing.events.fireMouseDownEvent(handle);
  186. goog.testing.events.fireMouseMoveEvent(handle);
  187. goog.testing.events.fireMouseUpEvent(handle);
  188. assertEquals('HANDLE_DRAG_END event expected', 1, handler.getCallCount());
  189. splitpane.setContinuousResize(false);
  190. handler.reset();
  191. goog.testing.events.fireMouseDownEvent(handle);
  192. goog.testing.events.fireMouseMoveEvent(handle);
  193. goog.testing.events.fireMouseUpEvent(handle);
  194. assertEquals('HANDLE_DRAG_END event expected', 1, handler.getCallCount());
  195. }
  196. function testSnapEvent() {
  197. splitpane.setInitialSize(200);
  198. splitpane.setHandleSize(10);
  199. splitpane.render(goog.dom.getElement('sandbox'));
  200. var handler = goog.testing.recordFunction();
  201. goog.events.listen(
  202. splitpane, goog.ui.SplitPane.EventType.HANDLE_SNAP, handler);
  203. var handle = goog.dom.getElementsByTagNameAndClass(
  204. goog.dom.TagName.DIV, 'goog-splitpane-handle')[0];
  205. goog.testing.events.fireDoubleClickSequence(handle);
  206. assertEquals('HANDLE_SNAP event expected', 1, handler.getCallCount());
  207. }