001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package javax.swing.plaf.basic;
018:
019: import java.awt.Canvas;
020: import java.awt.Component;
021: import java.awt.Dimension;
022: import java.awt.Insets;
023: import java.awt.Rectangle;
024: import javax.swing.BorderFactory;
025: import javax.swing.JButton;
026: import javax.swing.JComponent;
027: import javax.swing.JSplitPane;
028: import javax.swing.JViewport;
029: import javax.swing.SwingTestCase;
030:
031: public class BasicSplitPaneUITest extends SwingTestCase {
032: private JSplitPane splitPane;
033:
034: private BasicSplitPaneUI ui;
035:
036: @Override
037: protected void setUp() throws Exception {
038: splitPane = new JSplitPane();
039: ui = new BasicSplitPaneUI();
040: splitPane.setUI(ui);
041: splitPane.setSize(new Dimension(2000, 1000));
042: }
043:
044: @Override
045: protected void tearDown() throws Exception {
046: splitPane = null;
047: ui = null;
048: }
049:
050: public void testPreferredLayoutSize() throws Exception {
051: splitPane
052: .setBorder(BorderFactory.createEmptyBorder(5, 6, 7, 8));
053: ui.layoutManager.layoutContainer(splitPane);
054: int width = splitPane.getInsets().left
055: + splitPane.getInsets().right
056: + ui.layoutManager.components[0].getPreferredSize().width
057: + ui.layoutManager.components[1].getPreferredSize().width
058: + ui.layoutManager.components[2].getPreferredSize().width;
059: int height = splitPane.getInsets().top
060: + splitPane.getInsets().bottom
061: + ui.layoutManager.components[0].getPreferredSize().height;
062: assertEquals(new Dimension(width, height), ui.layoutManager
063: .preferredLayoutSize(splitPane));
064: splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
065: width = splitPane.getInsets().left
066: + splitPane.getInsets().right
067: + ui.layoutManager.components[1].getPreferredSize().width;
068: height = splitPane.getInsets().top
069: + splitPane.getInsets().bottom
070: + ui.layoutManager.components[0].getPreferredSize().height
071: + ui.layoutManager.components[1].getPreferredSize().height
072: + ui.layoutManager.components[2].getPreferredSize().height;
073: assertEquals(new Dimension(width, height), ui.layoutManager
074: .preferredLayoutSize(splitPane));
075: }
076:
077: public void testMinLayoutSize() throws Exception {
078: splitPane
079: .setBorder(BorderFactory.createEmptyBorder(5, 6, 7, 8));
080: ui.layoutManager.layoutContainer(splitPane);
081: int width = splitPane.getInsets().left
082: + splitPane.getInsets().right
083: + ui.layoutManager.components[0].getMinimumSize().width
084: + ui.layoutManager.components[1].getMinimumSize().width
085: + ui.layoutManager.components[2].getMinimumSize().width;
086: int height = splitPane.getInsets().top
087: + splitPane.getInsets().bottom
088: + ui.layoutManager.components[0].getMinimumSize().height;
089: assertEquals(new Dimension(width, height), ui.layoutManager
090: .minimumLayoutSize(splitPane));
091: splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
092: width = splitPane.getInsets().left
093: + splitPane.getInsets().right
094: + ui.layoutManager.components[1].getMinimumSize().width;
095: height = splitPane.getInsets().top
096: + splitPane.getInsets().bottom
097: + ui.layoutManager.components[0].getMinimumSize().height
098: + ui.layoutManager.components[1].getMinimumSize().height
099: + ui.layoutManager.components[2].getMinimumSize().height;
100: assertEquals(new Dimension(width, height), ui.layoutManager
101: .minimumLayoutSize(splitPane));
102: }
103:
104: public void testCreateUI() throws Exception {
105: assertNotNull(BasicSplitPaneUI.createUI(splitPane));
106: assertFalse(BasicSplitPaneUI.createUI(splitPane) == BasicSplitPaneUI
107: .createUI(splitPane));
108: }
109:
110: public void testCreatePropertyHandler() throws Exception {
111: assertNotNull(ui.createPropertyChangeListener());
112: if (isHarmony()) {
113: assertFalse(ui.createPropertyChangeListener() == ui
114: .createPropertyChangeListener());
115: }
116: }
117:
118: public void testCreateFocusHandler() throws Exception {
119: assertNotNull(ui.createFocusListener());
120: if (isHarmony()) {
121: assertFalse(ui.createFocusListener() == ui
122: .createFocusListener());
123: }
124: }
125:
126: public void testSetOrientation() throws Exception {
127: propertyChangeController = new PropertyChangeController();
128: splitPane.addPropertyChangeListener(propertyChangeController);
129: ui = (BasicSplitPaneUI) splitPane.getUI();
130: assertEquals(JSplitPane.HORIZONTAL_SPLIT, splitPane
131: .getOrientation());
132: assertEquals(JSplitPane.HORIZONTAL_SPLIT, ui.getOrientation());
133: ui.setOrientation(JSplitPane.VERTICAL_SPLIT);
134: assertEquals(JSplitPane.VERTICAL_SPLIT, ui.getOrientation());
135: assertFalse(JSplitPane.VERTICAL_SPLIT == splitPane
136: .getOrientation());
137: ui.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
138: propertyChangeController.reset();
139: splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
140: assertTrue(propertyChangeController.isChanged("orientation"));
141: assertEquals(JSplitPane.VERTICAL_SPLIT, ui.getOrientation());
142: assertEquals(JSplitPane.VERTICAL_SPLIT, splitPane
143: .getOrientation());
144: }
145:
146: public void testSetContinuousLayout() throws Exception {
147: propertyChangeController = new PropertyChangeController();
148: splitPane.addPropertyChangeListener(propertyChangeController);
149: assertFalse(splitPane.isContinuousLayout());
150: assertFalse(ui.isContinuousLayout());
151: ui.setContinuousLayout(true);
152: assertTrue(ui.isContinuousLayout());
153: assertFalse(splitPane.isContinuousLayout());
154: ui.setContinuousLayout(false);
155: assertFalse(ui.isContinuousLayout());
156: assertFalse(splitPane.isContinuousLayout());
157: propertyChangeController.reset();
158: splitPane.setContinuousLayout(true);
159: assertTrue(propertyChangeController
160: .isChanged("continuousLayout"));
161: assertTrue(ui.isContinuousLayout());
162: assertTrue(splitPane.isContinuousLayout());
163: }
164:
165: public void testSetLastDragLocation() throws Exception {
166: assertEquals(-1, ui.getLastDragLocation());
167: ui.setLastDragLocation(40);
168: assertEquals(40, ui.getLastDragLocation());
169: }
170:
171: // Regression for HARMONY-2771
172: public void testGetLastDragLocation() throws Exception {
173: assertEquals(0, new BasicSplitPaneUI().getLastDragLocation());
174: }
175:
176: public void testGetDivider() throws Exception {
177: assertNotNull(ui.getDivider());
178: assertNull(new BasicSplitPaneUI().getDivider());
179: }
180:
181: public void testGetSplitPane() throws Exception {
182: assertNotNull(ui.getSplitPane());
183: assertTrue(splitPane == ui.getSplitPane());
184: assertNull(new BasicSplitPaneUI().getSplitPane());
185: }
186:
187: public void testCreateDefaultNonContinuousLayoutDivider()
188: throws Exception {
189: assertTrue(ui.createDefaultNonContinuousLayoutDivider() instanceof Canvas);
190: }
191:
192: public void testCreateDefaultDivider() throws Exception {
193: assertNotNull(ui.createDefaultDivider());
194: }
195:
196: public void testSetNonContinuousLayoutDivider() throws Exception {
197: Component component = new Component() {
198: private static final long serialVersionUID = 1L;
199: };
200: ui.setNonContinuousLayoutDivider(component);
201: assertTrue(component == ui.getNonContinuousLayoutDivider());
202: assertFalse(ui.layoutManager.components[0] == component);
203: assertFalse(ui.layoutManager.components[1] == component);
204: assertFalse(ui.layoutManager.components[2] == component);
205: assertTrue(ui.nonContinuousLayoutDivider == component);
206: }
207:
208: public void testAddComponentToLayout() throws Exception {
209: assertEquals(3, ui.layoutManager.components.length);
210: JViewport viewport = new JViewport();
211: ui.layoutManager
212: .addLayoutComponent(JSplitPane.BOTTOM, viewport);
213: assertFalse(ui.layoutManager.components[0] == viewport);
214: assertTrue(ui.layoutManager.components[1] == viewport);
215: assertFalse(ui.layoutManager.components[2] == viewport);
216: ui.layoutManager.addLayoutComponent(JSplitPane.BOTTOM,
217: new JButton());
218: assertFalse(ui.layoutManager.components[1] == viewport);
219: ui.layoutManager.addLayoutComponent(JSplitPane.RIGHT, viewport);
220: assertFalse(ui.layoutManager.components[0] == viewport);
221: assertTrue(ui.layoutManager.components[1] == viewport);
222: assertFalse(ui.layoutManager.components[2] == viewport);
223: ui.layoutManager.addLayoutComponent(JSplitPane.BOTTOM,
224: new JButton());
225: ui.layoutManager.addLayoutComponent(JSplitPane.TOP, viewport);
226: assertTrue(ui.layoutManager.components[0] == viewport);
227: assertFalse(ui.layoutManager.components[1] == viewport);
228: assertFalse(ui.layoutManager.components[2] == viewport);
229: ui.layoutManager.addLayoutComponent(JSplitPane.TOP,
230: new JButton());
231: assertFalse(ui.layoutManager.components[0] == viewport);
232: ui.layoutManager.addLayoutComponent(JSplitPane.LEFT, viewport);
233: assertTrue(ui.layoutManager.components[0] == viewport);
234: assertFalse(ui.layoutManager.components[1] == viewport);
235: assertFalse(ui.layoutManager.components[2] == viewport);
236: ui.layoutManager.removeLayoutComponent(new JButton());
237: assertNotNull(ui.layoutManager.components[0]);
238: assertNotNull(ui.layoutManager.components[1]);
239: assertNotNull(ui.layoutManager.components[2]);
240: ui.layoutManager
241: .removeLayoutComponent(ui.layoutManager.components[2]);
242: assertNotNull(ui.layoutManager.components[0]);
243: assertNotNull(ui.layoutManager.components[1]);
244: assertNull(ui.layoutManager.components[2]);
245: ui.layoutManager
246: .removeLayoutComponent(ui.layoutManager.components[1]);
247: assertNotNull(ui.layoutManager.components[0]);
248: assertNull(ui.layoutManager.components[1]);
249: assertNull(ui.layoutManager.components[2]);
250: ui.layoutManager
251: .removeLayoutComponent(ui.layoutManager.components[0]);
252: assertNull(ui.layoutManager.components[0]);
253: assertNull(ui.layoutManager.components[1]);
254: assertNull(ui.layoutManager.components[2]);
255: try {
256: ui.layoutManager.addLayoutComponent("a", viewport);
257: fail("IllegalArgumentException should be thrown");
258: } catch (IllegalArgumentException e) {
259: }
260: }
261:
262: public void testGetComponentPreferredSize() throws Exception {
263: assertEquals(
264: ui.layoutManager.components[0].getPreferredSize().width,
265: ui.layoutManager
266: .getPreferredSizeOfComponent(ui.layoutManager.components[0]));
267: splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
268: assertEquals(
269: ui.layoutManager.components[0].getPreferredSize().height,
270: ui.layoutManager
271: .getPreferredSizeOfComponent(ui.layoutManager.components[0]));
272: }
273:
274: public void testGetComponentSize() throws Exception {
275: ui.layoutManager.layoutContainer(splitPane);
276: assertEquals(
277: ui.layoutManager.components[0].getSize().width,
278: ui.layoutManager
279: .getSizeOfComponent(ui.layoutManager.components[0]));
280: splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
281: assertEquals(
282: ui.layoutManager.components[0].getSize().height,
283: ui.layoutManager
284: .getSizeOfComponent(ui.layoutManager.components[0]));
285: }
286:
287: public void testGetAvailableSize() throws Exception {
288: splitPane.setSize(1000, 2000);
289: splitPane.setBorder(BorderFactory.createEmptyBorder(5, 10, 15,
290: 20));
291: assertEquals(splitPane.getSize().width - 10 - 20,
292: ui.layoutManager.getAvailableSize(splitPane.getSize(),
293: splitPane.getInsets()));
294: splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
295: assertEquals(splitPane.getSize().height - 5 - 15,
296: ui.layoutManager.getAvailableSize(splitPane.getSize(),
297: splitPane.getInsets()));
298: }
299:
300: public void testResetPreferredSizes() throws Exception {
301: if (isHarmony()) {
302: assertEquals(0, ui.getDividerLocation(splitPane));
303: ui.resetToPreferredSizes(splitPane);
304: assertEquals(splitPane.getLeftComponent()
305: .getPreferredSize().width, ui
306: .getDividerLocation(splitPane));
307: }
308: }
309:
310: public void testMinMaxDividerLocation() throws Exception {
311: splitPane.setBorder(BorderFactory.createEmptyBorder(5, 10, 15,
312: 20));
313: assertEquals(
314: splitPane.getLeftComponent().getPreferredSize().width
315: + splitPane.getInsets().left, ui
316: .getMinimumDividerLocation(splitPane));
317: assertEquals(splitPane.getWidth() - splitPane.getDividerSize()
318: - splitPane.getRightComponent().getMinimumSize().width
319: - splitPane.getInsets().right, ui
320: .getMaximumDividerLocation(splitPane));
321: }
322:
323: public void testSetDividerLocation() throws Exception {
324: ui.setDividerLocation(splitPane, 230);
325: assertEquals(-1, splitPane.getDividerLocation());
326: assertEquals(0, ui.getDividerLocation(splitPane));
327: splitPane.setDividerLocation(20);
328: assertEquals(20, splitPane.getDividerLocation());
329: assertEquals(0, ui.getDividerLocation(splitPane));
330: ui.layoutManager.layoutContainer(splitPane);
331: assertEquals(20, ui.getDividerLocation(splitPane));
332: }
333:
334: public void testGetDividerLocation() throws Exception {
335: try { // Regression test for HARMONY-2661
336: ui.getDividerLocation(null);
337: fail("NullPointerException should have been thrown");
338: } catch (NullPointerException e) {
339: // Expected
340: }
341: }
342:
343: public void testInitialLocation() throws Exception {
344: splitPane.setSize(1000, 2000);
345: splitPane.setBorder(BorderFactory.createEmptyBorder(5, 10, 15,
346: 20));
347: assertEquals(splitPane.getInsets().left, ui.layoutManager
348: .getInitialLocation(splitPane.getInsets()));
349: splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
350: assertEquals(splitPane.getInsets().top, ui.layoutManager
351: .getInitialLocation(splitPane.getInsets()));
352: }
353:
354: public void testUpdateComponents() throws Exception {
355: ui.layoutManager.components[0] = null;
356: ui.layoutManager.components[1] = null;
357: ui.layoutManager.components[2] = null;
358: ui.layoutManager.updateComponents();
359: assertEquals(splitPane.getLeftComponent(),
360: ui.layoutManager.components[0]);
361: assertEquals(splitPane.getRightComponent(),
362: ui.layoutManager.components[1]);
363: assertEquals(ui.getDivider(), ui.layoutManager.components[2]);
364: }
365:
366: public void testSetComponentToSize() throws Exception {
367: JButton b = new JButton();
368: b.setSize(new Dimension(20, 30));
369: int size = 5;
370: int location = 15;
371: int cW = 50;
372: int cH = 40;
373: Insets insets = new Insets(1, 2, 3, 4);
374: ui.layoutManager.setComponentToSize(b, size, location, insets,
375: new Dimension(cW, cH));
376: assertEquals(new Rectangle(location, insets.top, size, cH
377: - insets.top - insets.bottom), b.getBounds());
378: splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
379: ui.layoutManager.setComponentToSize(b, size, location, insets,
380: new Dimension(cW, cH));
381: assertEquals(new Rectangle(insets.left, location, cW
382: - insets.left - insets.right, size), b.getBounds());
383: }
384:
385: public void testGetSizes() { // Regression test for HARMONY-2767
386: ui = new BasicSplitPaneUI();
387: JComponent component = new JComponent() {
388: };
389: assertEquals(new Dimension(0, 0), ui
390: .getPreferredSize(component));
391: assertEquals(new Dimension(0, 0), ui.getPreferredSize(null));
392: assertEquals(new Dimension(0, 0), ui.getMinimumSize(component));
393: assertEquals(new Dimension(0, 0), ui.getMinimumSize(null));
394: assertEquals(new Dimension(0, 0), ui.getMaximumSize(component));
395: assertEquals(new Dimension(0, 0), ui.getMaximumSize(null));
396: }
397: }
|