KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > tabcontrol > plaf > LayoutModelTest


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.swing.tabcontrol.plaf;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Graphics JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import javax.swing.BorderFactory JavaDoc;
27 import javax.swing.Icon JavaDoc;
28 import javax.swing.JComponent JavaDoc;
29 import javax.swing.JLabel JavaDoc;
30 import junit.framework.TestCase;
31 import org.netbeans.swing.tabcontrol.DefaultTabDataModel;
32 import org.netbeans.swing.tabcontrol.TabData;
33 import org.netbeans.swing.tabcontrol.TabDataModel;
34
35 /** Tests for all of the functionality of TabLayoutModel instances
36  *
37  * @author Tim Boudreau
38  */

39 public class LayoutModelTest extends TestCase {
40     DefaultTabDataModel mdl=null;
41     DefaultTabSelectionModel sel = null;
42     TestLayoutModel lay = null;
43
44     public LayoutModelTest(String JavaDoc testName) {
45         super(testName);
46     }
47     
48     public void setUp() {
49         prepareModel();
50     }
51     
52     Icon JavaDoc ic = new Icon JavaDoc () {
53         public int getIconWidth() {
54             return 16;
55         }
56         public int getIconHeight() {
57             return 16;
58         }
59         public void paintIcon (Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
60             //do nothing
61
}
62     };
63     
64     Icon JavaDoc sameSizeIcon = new Icon JavaDoc () {
65         public int getIconWidth() {
66             return 16;
67         }
68         public int getIconHeight() {
69             return 16;
70         }
71         public void paintIcon (Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
72             //do nothing
73
}
74     };
75     
76     Icon JavaDoc biggerIcon = new Icon JavaDoc () {
77         public int getIconWidth() {
78             return 22;
79         }
80         public int getIconHeight() {
81             return 22;
82         }
83         public void paintIcon (Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
84             //do nothing
85
}
86     };
87     
88     /** Weird, but this class was adapted from a standalone test written
89      * long ago and rescued from cvs history. It didn't use JUnit, and
90      * the assertTrue argument order was reversed. So in the interest of
91      * laziness... */

92     private void assertPravda (boolean val, String JavaDoc msg) {
93         assertTrue (msg, val);
94     }
95     
96     int padX;
97     int padY;
98     private void prepareModel() {
99         TabData[] td = new TabData[25];
100         int ct = 0;
101         for (char c='a'; c < 'z'; c++) {
102             char[] ch = new char[ct+1];
103             Arrays.fill (ch, c);
104             String JavaDoc name = new String JavaDoc (ch);
105             Component JavaDoc comp = new JLabel JavaDoc(name);
106             comp.setName (name);
107             td[ct] = new TabData (comp, ic, name, "tip:"+name);
108             ct++;
109         }
110         padX = 2;
111         padY = 2;
112         mdl = new DefaultTabDataModel (td);
113         JLabel JavaDoc jl = new JLabel JavaDoc();
114         jl.setBorder (BorderFactory.createEmptyBorder());
115         lay = new TestLayoutModel (mdl, jl);
116         lay.setPadding (new Dimension JavaDoc(padX, padY));
117     }
118     
119     /*
120     public void run() {
121         testSizes();
122         testRemoval();
123         System.err.println("All tests passed for layout model");
124     }
125      */

126     
127     public void testSizes() {
128         System.err.println("testSizes");
129         int pos=0;
130         for (int i=0; i < mdl.size(); i++) {
131             int expectedSize = ic.getIconWidth() + i + padX + 1;
132             assertPravda (lay.getW(i) == expectedSize, "Width of " + (i+1) + " - "
133             + mdl.getTab(i).getText() + " should be " + expectedSize + " but is "
134             + lay.getW(i));
135             assertPravda (pos == lay.getX(i), "X at " + i + " should be " + pos + " but is " + lay.getX(i));
136             pos += lay.getW(i);
137         }
138     }
139     
140     public void testRemoval() {
141         System.err.println("testRemoval");
142         mdl.removeTab (0);
143         int expectedSize = ic.getIconWidth() + 2 + padX;
144         assertPravda (lay.getW(0) == expectedSize, "Removed item at 0, new 0 item not correct size");
145     }
146     
147
148     /** A default model subclass that uses character count for width for testing */
149     class TestLayoutModel extends BaseTabLayoutModel {
150         public TestLayoutModel(TabDataModel model, JComponent JavaDoc target) {
151             super (model, new JLabel JavaDoc());
152         }
153         
154         protected int textWidth (int index) {
155             return model.getTab (index).getText().length();
156         }
157         
158         protected int textHeight (int index) {
159             return 16;
160         }
161     }
162     
163 }
164
Popular Tags