001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.swing.tabcontrol.plaf;
043:
044: import java.awt.Component;
045: import java.awt.Dimension;
046: import java.awt.Graphics;
047: import java.util.Arrays;
048: import javax.swing.BorderFactory;
049: import javax.swing.Icon;
050: import javax.swing.JComponent;
051: import javax.swing.JLabel;
052: import junit.framework.TestCase;
053: import org.netbeans.swing.tabcontrol.DefaultTabDataModel;
054: import org.netbeans.swing.tabcontrol.TabData;
055: import org.netbeans.swing.tabcontrol.TabDataModel;
056:
057: /** Tests for all of the functionality of TabLayoutModel instances
058: *
059: * @author Tim Boudreau
060: */
061: public class LayoutModelTest extends TestCase {
062: DefaultTabDataModel mdl = null;
063: DefaultTabSelectionModel sel = null;
064: TestLayoutModel lay = null;
065:
066: public LayoutModelTest(String testName) {
067: super (testName);
068: }
069:
070: public void setUp() {
071: prepareModel();
072: }
073:
074: Icon ic = new Icon() {
075: public int getIconWidth() {
076: return 16;
077: }
078:
079: public int getIconHeight() {
080: return 16;
081: }
082:
083: public void paintIcon(Component c, Graphics g, int x, int y) {
084: //do nothing
085: }
086: };
087:
088: Icon sameSizeIcon = new Icon() {
089: public int getIconWidth() {
090: return 16;
091: }
092:
093: public int getIconHeight() {
094: return 16;
095: }
096:
097: public void paintIcon(Component c, Graphics g, int x, int y) {
098: //do nothing
099: }
100: };
101:
102: Icon biggerIcon = new Icon() {
103: public int getIconWidth() {
104: return 22;
105: }
106:
107: public int getIconHeight() {
108: return 22;
109: }
110:
111: public void paintIcon(Component c, Graphics g, int x, int y) {
112: //do nothing
113: }
114: };
115:
116: /** Weird, but this class was adapted from a standalone test written
117: * long ago and rescued from cvs history. It didn't use JUnit, and
118: * the assertTrue argument order was reversed. So in the interest of
119: * laziness... */
120: private void assertPravda(boolean val, String msg) {
121: assertTrue(msg, val);
122: }
123:
124: int padX;
125: int padY;
126:
127: private void prepareModel() {
128: TabData[] td = new TabData[25];
129: int ct = 0;
130: for (char c = 'a'; c < 'z'; c++) {
131: char[] ch = new char[ct + 1];
132: Arrays.fill(ch, c);
133: String name = new String(ch);
134: Component comp = new JLabel(name);
135: comp.setName(name);
136: td[ct] = new TabData(comp, ic, name, "tip:" + name);
137: ct++;
138: }
139: padX = 2;
140: padY = 2;
141: mdl = new DefaultTabDataModel(td);
142: JLabel jl = new JLabel();
143: jl.setBorder(BorderFactory.createEmptyBorder());
144: lay = new TestLayoutModel(mdl, jl);
145: lay.setPadding(new Dimension(padX, padY));
146: }
147:
148: /*
149: public void run() {
150: testSizes();
151: testRemoval();
152: System.err.println("All tests passed for layout model");
153: }
154: */
155:
156: public void testSizes() {
157: System.err.println("testSizes");
158: int pos = 0;
159: for (int i = 0; i < mdl.size(); i++) {
160: int expectedSize = ic.getIconWidth() + i + padX + 1;
161: assertPravda(lay.getW(i) == expectedSize, "Width of "
162: + (i + 1) + " - " + mdl.getTab(i).getText()
163: + " should be " + expectedSize + " but is "
164: + lay.getW(i));
165: assertPravda(pos == lay.getX(i), "X at " + i
166: + " should be " + pos + " but is " + lay.getX(i));
167: pos += lay.getW(i);
168: }
169: }
170:
171: public void testRemoval() {
172: System.err.println("testRemoval");
173: mdl.removeTab(0);
174: int expectedSize = ic.getIconWidth() + 2 + padX;
175: assertPravda(lay.getW(0) == expectedSize,
176: "Removed item at 0, new 0 item not correct size");
177: }
178:
179: /** A default model subclass that uses character count for width for testing */
180: class TestLayoutModel extends BaseTabLayoutModel {
181: public TestLayoutModel(TabDataModel model, JComponent target) {
182: super (model, new JLabel());
183: }
184:
185: protected int textWidth(int index) {
186: return model.getTab(index).getText().length();
187: }
188:
189: protected int textHeight(int index) {
190: return 16;
191: }
192: }
193:
194: }
|