1 19 20 package org.netbeans.swing.tabcontrol.plaf; 21 22 import java.awt.Component ; 23 import java.awt.Dimension ; 24 import java.awt.Graphics ; 25 import java.util.Arrays ; 26 import javax.swing.BorderFactory ; 27 import javax.swing.Icon ; 28 import javax.swing.JComponent ; 29 import javax.swing.JLabel ; 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 39 public class LayoutModelTest extends TestCase { 40 DefaultTabDataModel mdl=null; 41 DefaultTabSelectionModel sel = null; 42 TestLayoutModel lay = null; 43 44 public LayoutModelTest(String testName) { 45 super(testName); 46 } 47 48 public void setUp() { 49 prepareModel(); 50 } 51 52 Icon ic = new Icon () { 53 public int getIconWidth() { 54 return 16; 55 } 56 public int getIconHeight() { 57 return 16; 58 } 59 public void paintIcon (Component c, Graphics g, int x, int y) { 60 } 62 }; 63 64 Icon sameSizeIcon = new Icon () { 65 public int getIconWidth() { 66 return 16; 67 } 68 public int getIconHeight() { 69 return 16; 70 } 71 public void paintIcon (Component c, Graphics g, int x, int y) { 72 } 74 }; 75 76 Icon biggerIcon = new Icon () { 77 public int getIconWidth() { 78 return 22; 79 } 80 public int getIconHeight() { 81 return 22; 82 } 83 public void paintIcon (Component c, Graphics g, int x, int y) { 84 } 86 }; 87 88 92 private void assertPravda (boolean val, String 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 name = new String (ch); 105 Component comp = new JLabel (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 jl = new JLabel (); 114 jl.setBorder (BorderFactory.createEmptyBorder()); 115 lay = new TestLayoutModel (mdl, jl); 116 lay.setPadding (new Dimension (padX, padY)); 117 } 118 119 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 149 class TestLayoutModel extends BaseTabLayoutModel { 150 public TestLayoutModel(TabDataModel model, JComponent target) { 151 super (model, new JLabel ()); 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 |