KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > view > ui > MultiSplitPaneTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.windows.view.ui;
21
22 import javax.swing.JSplitPane JavaDoc;
23 import javax.swing.JWindow JavaDoc;
24 import org.netbeans.junit.NbTestCase;
25
26 /**
27  * Some basic tests for MultiSplitPane class to verify its resizing behavior.
28  *
29  * @author Stanislav Aubrecht
30  */

31 public class MultiSplitPaneTest extends NbTestCase {
32
33     private static int DIVIDER_SIZE = 10;
34     private MultiSplitPane split;
35     private JWindow JavaDoc testWindow;
36
37     public MultiSplitPaneTest(String JavaDoc testName) {
38         super(testName);
39     }
40
41     protected boolean runInEQ () {
42         return true;
43     }
44
45     protected void setUp() throws Exception JavaDoc {
46         split = new MultiSplitPane();
47         split.setDividerSize( DIVIDER_SIZE );
48         
49         testWindow = new JWindow JavaDoc();
50         testWindow.setVisible( true );
51         //testWindow.getContentPane().add( split );
52
}
53
54     protected void resizeSplit( int orientation, int splitSize, int nonResizingSize ) {
55         if( orientation == JSplitPane.HORIZONTAL_SPLIT )
56             split.setSize( splitSize, nonResizingSize );
57         else
58             split.setSize( nonResizingSize, splitSize );
59         testWindow.getContentPane().add( split );
60         split.validate();
61     }
62     
63     protected void checkNonSplitSizes( int expectedSize, TestViewElement[] views ) {
64         for( int i=0; i<views.length; i++ ) {
65             assertEquals( "Invalid non-split size for view " + i, expectedSize, views[i].getNonSplitSize() );
66         }
67     }
68
69     public void testProportionalResize2ChildrenHorizontal() {
70         doTestProportionalResize2Children( JSplitPane.HORIZONTAL_SPLIT );
71     }
72     
73     public void testProportionalResize2ChildrenVertical() {
74         doTestProportionalResize2Children( JSplitPane.VERTICAL_SPLIT );
75     }
76
77     protected void doTestProportionalResize2Children( int orientation ) {
78         TestViewElement[] views = new TestViewElement[2];
79         double[] splitWeights = new double[2];
80         
81         views[0] = new TestViewElement( orientation, 0.0 );
82         splitWeights[0] = 0.5;
83         views[1] = new TestViewElement( orientation, 0.0 );
84         splitWeights[1] = 0.5;
85
86         split.setChildren( orientation, views, splitWeights );
87         
88         //initial resizing, children sizes must honour their initial split weights
89
resizeSplit( orientation, 200+DIVIDER_SIZE, 200 );
90         assertEquals( 100, views[0].getSizeInSplit() );
91         assertEquals( 100, views[1].getSizeInSplit() );
92         //check that children height matches the height of the split
93
checkNonSplitSizes( 200, views );
94
95         //children must shrink proportionally
96
resizeSplit( orientation, 100+DIVIDER_SIZE, 300 );
97         assertEquals( 50, views[0].getSizeInSplit() );
98         assertEquals( 50, views[1].getSizeInSplit() );
99         checkNonSplitSizes( 300, views );
100         
101         //minimum sizes must be honoured
102
views[0].setMinSize( 10 );
103         views[1].setMinSize( 10 );
104         resizeSplit( orientation, 0, 100 );
105         assertEquals( 10, views[0].getSizeInSplit() );
106         assertEquals( 10, views[1].getSizeInSplit() );
107         checkNonSplitSizes( 100, views );
108     }
109
110     public void testProportionalResize5ChildrenHorizontal() {
111         doTestProportionalResize5Children( JSplitPane.HORIZONTAL_SPLIT );
112     }
113     
114     public void testProportionalResize5ChildrenVertical() {
115         doTestProportionalResize5Children( JSplitPane.VERTICAL_SPLIT );
116     }
117
118     protected void doTestProportionalResize5Children( int orientation ) {
119         TestViewElement[] views = new TestViewElement[5];
120         double[] splitWeights = new double[5];
121         
122         views[0] = new TestViewElement( orientation, 0.0 );
123         splitWeights[0] = 0.1;
124         views[1] = new TestViewElement( orientation, 0.0 );
125         splitWeights[1] = 0.1;
126         views[2] = new TestViewElement( orientation, 0.0 );
127         splitWeights[2] = 0.1;
128         views[3] = new TestViewElement( orientation, 0.0 );
129         splitWeights[3] = 0.1;
130         views[4] = new TestViewElement( orientation, 0.0 );
131         splitWeights[4] = 0.6;
132
133         split.setChildren( orientation, views, splitWeights );
134         
135         //initial resizing, children sizes must honour their initial split weights
136
resizeSplit( orientation, 1000+4*DIVIDER_SIZE, 200 );
137         assertEquals( 100, views[0].getSizeInSplit() );
138         assertEquals( 100, views[1].getSizeInSplit() );
139         assertEquals( 100, views[2].getSizeInSplit() );
140         assertEquals( 100, views[3].getSizeInSplit() );
141         assertEquals( 600, views[4].getSizeInSplit() );
142         //check that children height matches the height of the split
143
checkNonSplitSizes( 200, views );
144
145         //children must shrink proportionally
146
resizeSplit( orientation, 100+4*DIVIDER_SIZE, 300 );
147         assertEquals( 10, views[0].getSizeInSplit() );
148         assertEquals( 10, views[1].getSizeInSplit() );
149         assertEquals( 10, views[2].getSizeInSplit() );
150         assertEquals( 10, views[3].getSizeInSplit() );
151         assertEquals( 60, views[4].getSizeInSplit() );
152         checkNonSplitSizes( 300, views );
153         
154         //minimum sizes must be honoured
155
views[0].setMinSize( 10 );
156         views[1].setMinSize( 10 );
157         views[2].setMinSize( 10 );
158         views[3].setMinSize( 10 );
159         views[4].setMinSize( 10 );
160         resizeSplit( orientation, 0, 100 );
161         assertEquals( 10, views[0].getSizeInSplit() );
162         assertEquals( 10, views[1].getSizeInSplit() );
163         assertEquals( 10, views[2].getSizeInSplit() );
164         assertEquals( 10, views[3].getSizeInSplit() );
165         assertEquals( 10, views[4].getSizeInSplit() );
166         checkNonSplitSizes( 100, views );
167     }
168
169
170     public void testEqualResizeWeightsHorizontal() {
171         doTestEqualResizeWeights( JSplitPane.HORIZONTAL_SPLIT );
172     }
173     
174     public void testEqualResizeWeightsVertical() {
175         doTestEqualResizeWeights( JSplitPane.VERTICAL_SPLIT );
176     }
177
178     protected void doTestEqualResizeWeights( int orientation ) {
179         TestViewElement[] views = new TestViewElement[4];
180         double[] splitWeights = new double[4];
181         
182         views[0] = new TestViewElement( orientation, 0.25 );
183         splitWeights[0] = 1.0;
184         views[1] = new TestViewElement( orientation, 0.25 );
185         splitWeights[1] = 1.0;
186         views[2] = new TestViewElement( orientation, 0.25 );
187         splitWeights[2] = 1.0;
188         views[3] = new TestViewElement( orientation, 0.25 );
189         splitWeights[3] = 1.0;
190
191         split.setChildren( orientation, views, splitWeights );
192         
193         //initial resizing, children sizes must honour their initial split weights
194
resizeSplit( orientation, 1000+3*DIVIDER_SIZE, 200 );
195         assertEquals( 250, views[0].getSizeInSplit() );
196         assertEquals( 250, views[1].getSizeInSplit() );
197         assertEquals( 250, views[2].getSizeInSplit() );
198         assertEquals( 250, views[3].getSizeInSplit() );
199         //check that children height matches the height of the split
200
checkNonSplitSizes( 200, views );
201
202         //children must shrink according to their resize weights
203
resizeSplit( orientation, 100+3*DIVIDER_SIZE, 300 );
204         assertEquals( 25, views[0].getSizeInSplit() );
205         assertEquals( 25, views[1].getSizeInSplit() );
206         assertEquals( 25, views[2].getSizeInSplit() );
207         assertEquals( 25, views[3].getSizeInSplit() );
208         checkNonSplitSizes( 300, views );
209         
210         //minimum sizes must be honoured
211
views[0].setMinSize( 10 );
212         views[1].setMinSize( 10 );
213         views[2].setMinSize( 10 );
214         views[3].setMinSize( 10 );
215         resizeSplit( orientation, 0, 100 );
216         assertEquals( 10, views[0].getSizeInSplit() );
217         assertEquals( 10, views[1].getSizeInSplit() );
218         assertEquals( 10, views[2].getSizeInSplit() );
219         assertEquals( 10, views[3].getSizeInSplit() );
220         checkNonSplitSizes( 100, views );
221     }
222
223     public void testDifferentResizeWeightsHorizontal() {
224         doTestDifferentResizeWeights( JSplitPane.HORIZONTAL_SPLIT );
225     }
226     
227     public void testDifferentResizeWeightsVertical() {
228         doTestDifferentResizeWeights( JSplitPane.VERTICAL_SPLIT );
229     }
230
231     protected void doTestDifferentResizeWeights( int orientation ) {
232         TestViewElement[] views = new TestViewElement[4];
233         double[] splitWeights = new double[4];
234         
235         views[0] = new TestViewElement( orientation, 0.2 ); //normalized to .1
236
splitWeights[0] = 1.0;
237         views[1] = new TestViewElement( orientation, 0.2 ); //normalized to .1
238
splitWeights[1] = 1.0;
239         views[2] = new TestViewElement( orientation, 0.5 ); //normalized to .4
240
splitWeights[2] = 1.0;
241         views[3] = new TestViewElement( orientation, 0.5 ); //normalized to .4
242
splitWeights[3] = 1.0;
243
244         split.setChildren( orientation, views, splitWeights );
245         
246         //initial resizing, children sizes must honour their initial split weights
247
resizeSplit( orientation, 100+3*DIVIDER_SIZE, 200 );
248         assertEquals( 25, views[0].getSizeInSplit() );
249         assertEquals( 25, views[1].getSizeInSplit() );
250         assertEquals( 25, views[2].getSizeInSplit() );
251         assertEquals( 25, views[3].getSizeInSplit() );
252         //check that children height matches the height of the split
253
checkNonSplitSizes( 200, views );
254
255         //children must grow according to their resize weights
256
resizeSplit( orientation, 1000+3*DIVIDER_SIZE, 300 );
257         assertEquals( 115, views[0].getSizeInSplit() ); //+900*.1
258
assertEquals( 115, views[1].getSizeInSplit() ); //+900*.1
259
assertEquals( 385, views[2].getSizeInSplit() ); //+900*.4
260
assertEquals( 385, views[3].getSizeInSplit() ); //+900*.4
261
checkNonSplitSizes( 300, views );
262         
263         //children must shrink according to their resize weights
264
resizeSplit( orientation, 100+3*DIVIDER_SIZE, 300 );
265         assertEquals( 12, views[0].getSizeInSplit() ); //this is a bug, the correct value is 25
266
assertEquals( 12, views[1].getSizeInSplit() ); //this is a bug, the correct value is 25
267
assertEquals( 39, views[2].getSizeInSplit() ); //this is a bug, the correct value is 25
268
assertEquals( 37, views[3].getSizeInSplit() ); //this is a bug, the correct value is 25
269
checkNonSplitSizes( 300, views );
270
271         //minimum sizes must be honoured
272
views[0].setMinSize( 10 );
273         views[1].setMinSize( 10 );
274         views[2].setMinSize( 10 );
275         views[3].setMinSize( 10 );
276         resizeSplit( orientation, 0, 100 );
277         assertEquals( 10, views[0].getSizeInSplit() );
278         assertEquals( 10, views[1].getSizeInSplit() );
279         assertEquals( 10, views[2].getSizeInSplit() );
280         assertEquals( 10, views[3].getSizeInSplit() );
281         checkNonSplitSizes( 100, views );
282     }
283 }
284
Popular Tags