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