1 19 20 package org.netbeans.swing.tabcontrol.plaf; 21 22 import java.awt.Component ; 23 import java.awt.Graphics ; 24 import javax.swing.Icon ; 25 import javax.swing.JLabel ; 26 import javax.swing.event.ChangeEvent ; 27 import javax.swing.event.ChangeListener ; 28 import javax.swing.event.ListDataEvent ; 29 import junit.framework.TestCase; 30 import org.netbeans.swing.tabcontrol.DefaultTabDataModel; 31 import org.netbeans.swing.tabcontrol.TabData; 32 import org.netbeans.swing.tabcontrol.TabDataModel; 33 import org.netbeans.swing.tabcontrol.event.ComplexListDataEvent; 34 import org.netbeans.swing.tabcontrol.event.ComplexListDataListener; 35 36 40 public class SelectionModelTest extends TestCase implements ComplexListDataListener, ChangeListener { 41 TabDataModel mdl; 42 DefaultTabSelectionModel sel; 43 String lastListenerCall=null; 44 ComplexListDataEvent lastEvent=null; 45 46 public SelectionModelTest(String testName) { 47 super(testName); 48 } 49 50 Icon ic = new Icon () { 51 public int getIconWidth() { 52 return 16; 53 } 54 public int getIconHeight() { 55 return 16; 56 } 57 public void paintIcon (Component c, Graphics g, int x, int y) { 58 } 60 }; 61 62 Icon sameSizeIcon = new Icon () { 63 public int getIconWidth() { 64 return 16; 65 } 66 public int getIconHeight() { 67 return 16; 68 } 69 public void paintIcon (Component c, Graphics g, int x, int y) { 70 } 72 }; 73 74 Icon biggerIcon = new Icon () { 75 public int getIconWidth() { 76 return 22; 77 } 78 public int getIconHeight() { 79 return 22; 80 } 81 public void paintIcon (Component c, Graphics g, int x, int y) { 82 } 84 }; 85 86 87 public void setUp() { 88 prepareModel(); 89 } 90 94 private void assertPravda (boolean val, String msg) { 95 assertTrue (msg, val); 96 } 97 98 private void assertEventFired () { 99 assertPravda (eventFired, "Event expected but none fired"); 100 eventFired = false; 101 } 102 103 private void prepareModel() { 104 if (mdl != null) { 105 mdl.removeComplexListDataListener(this); 106 } 107 TabData[] td = new TabData[25]; 108 int ct = 0; 109 for (char c='a'; c < 'z'; c++) { 110 String name = new String (new char[]{c}); 111 Component comp = new JLabel (name); 112 comp.setName (name); 113 td[ct] = new TabData (comp, ic, name, "tip:"+name); 114 ct++; 115 } 116 mdl = new DefaultTabDataModel (td); 117 sel = new DefaultTabSelectionModel (mdl); 119 sel.addChangeListener (this); 120 } 121 122 125 public void testEverything() { 126 doTestGetSelectedIndex(); 127 doTestSetSelectedIndex(); 128 doTestInsertSingle(); 129 doTestRemoveSingle(); 130 doTestInsertContiguous(); 131 141 System.err.println("All tests passed for selection model"); 142 } 143 144 public void doTestGetSelectedIndex() { 145 System.err.println("testGetSelectedIndex"); 146 int i = sel.getSelectedIndex(); 147 assertPravda (i == -1, "Initial selected index should be -1"); 148 } 149 150 public void doTestSetSelectedIndex() { 151 System.err.println("testSetSelectedIndex"); 152 eventFired = false; 153 sel.setSelectedIndex(5); 154 assertEventFired(); 155 assertPravda (5 == sel.getSelectedIndex(), "Selected index should be 5 but is " + sel.getSelectedIndex()); 156 noEvent = true; 158 sel.setSelectedIndex(5); 159 noEvent = false; 160 } 161 162 public void doTestInsertSingle () { 163 System.err.println("testInsertSingle"); 164 TabData td = new TabData (new JLabel (), ic, "insertData", "Tip:insertData"); 165 noEvent = true; 166 mdl.addTab(7, td); 167 noEvent = false; 168 assertPravda (5 == sel.getSelectedIndex(), "Selected index should be 5 but is " + sel.getSelectedIndex()); 169 TabData tdb = new TabData (new JLabel (), ic, "insertData2", "Tip:insertData2"); 170 mdl.addTab(3, tdb); 171 assertEventFired(); 172 assertPravda (6 == sel.getSelectedIndex(), "After insert, selected index should be 6 but is " + sel.getSelectedIndex()); 173 } 174 175 public void doTestRemoveSingle() { 176 System.err.println("testRemoveSingle"); 177 mdl.removeTab(3); 178 assertEventFired(); 179 assertPravda (5 == sel.getSelectedIndex(), "After remove, selected index should be 5 but is " + sel.getSelectedIndex()); 180 noEvent = true; 181 mdl.removeTab(7); 182 noEvent = false; 183 assertPravda (5 == sel.getSelectedIndex(), "After remove, selected index should be 5 but is " + sel.getSelectedIndex()); 184 } 186 187 public void doTestInsertContiguous () { 188 System.err.println("testInsertContiguous"); 189 190 TabData[] td = new TabData[5]; 191 for (int i=0; i < 5; i++) { 192 String nm = "ic" + Integer.toString(i); 193 td[i] = new TabData (new JLabel (), ic, nm, "tip:nm"); 194 } 195 196 int idx = sel.getSelectedIndex(); 197 mdl.addTabs(0, td); 198 assertEventFired(); 199 assertPravda (idx + 4 == sel.getSelectedIndex(), "After contiguous insert, selection should be " + (idx+5) + " but is " + sel.getSelectedIndex()); 200 noEvent=true; 201 mdl.addTabs (20, td); 202 noEvent = false; 203 } 204 205 public void doTestRemoveContiguous () { 206 System.err.println("testRemoveContiguous"); 207 noEvent = true; 208 mdl.removeTabs(20, 24); 209 noEvent = false; 210 mdl.removeTabs (2, 6); 211 assertEventFired(); 212 assertPravda (4 == sel.getSelectedIndex(), "After contiguous remove, selected index should be 5 but is " + sel.getSelectedIndex()); 213 } 214 215 public void doTestInsertNonContiguous () { 216 System.err.println("testInsertNonContiguous"); 217 sel.setSelectedIndex(10); 218 int[] indices = {1, 3, 5, 7}; 219 TabData[] td = new TabData[4]; 220 for (int i=0; i < 4; i++) { 221 String nm = "icnc" + Integer.toString(i); 222 td[i] = new TabData (new JLabel (), ic, nm, "tip:nm"); 223 } 224 mdl.addTabs(indices, td); 225 assertEventFired(); 226 assertPravda (14 == sel.getSelectedIndex(), "After non contiguous add of 4 items, selected index should be 14 but is " + sel.getSelectedIndex()); 227 228 indices = new int[] {16, 17, 21, 23}; 229 noEvent = true; 230 mdl.addTabs (indices, td); 231 noEvent = false; 232 assertPravda (14 == sel.getSelectedIndex(), "After non contiguous add above the selected index, selected index should still be 14 but is " + sel.getSelectedIndex()); 233 234 indices = new int[] {3, 5, 22, 23}; 235 mdl.addTabs (indices, td); 236 assertEventFired(); 237 assertPravda (16 == sel.getSelectedIndex(), "After non contiguous add straddling the selected index, selected index should still be 16 but is " + sel.getSelectedIndex()); 238 } 239 240 public void doTestRemoveNonContiguous() { 241 System.err.println("testRemoveNonContiguous"); 242 sel.setSelectedIndex (10); 243 int[] indices = {1, 3, 5, 7}; 244 mdl.removeTabs(indices); 245 assertEventFired(); 246 assertPravda (5 == sel.getSelectedIndex(), "After non contiguous remove before the selected index, selected index should still be 6 but is " + sel.getSelectedIndex()); 247 248 indices = new int[] {13, 15, 17}; 249 noEvent = true; 250 mdl.removeTabs (indices); 251 noEvent = false; 252 assertPravda (5 == sel.getSelectedIndex(), "After non contiguous remove above the selected index, selected index should still be 6 but is " + sel.getSelectedIndex()); 253 254 indices = new int[] {2, 5, 18, 19}; 255 mdl.removeTabs(indices); 256 assertEventFired(); 257 assertPravda (4 == sel.getSelectedIndex(), "After non contiguous remove before the selected index, selected index should still be 6 but is " + sel.getSelectedIndex()); 258 } 259 260 public void doTestSet () { 261 System.err.println("testSet"); 262 int i = sel.getSelectedIndex(); 263 TabData td = new TabData (new JLabel (), ic, "inserted", "tip:inserted"); 264 noEvent = true; 265 mdl.setTab(i, td); 266 mdl.setTab (i-3, td); 267 mdl.setTab (i+5, td); 268 noEvent = false; 269 } 270 271 public void doTestRemoveSelected () { 272 System.err.println("testRemoveSelected"); 273 prepareModel(); 274 int i = mdl.size() - 5; 275 sel.setSelectedIndex(i); 276 mdl.removeTab(i); 277 assertEventFired(); 278 assertPravda (i == sel.getSelectedIndex(), "After remove of the selected index when not at edge, selected index should still be " + i + " but is " + sel.getSelectedIndex()); 279 280 sel.setSelectedIndex (mdl.size()-1); 281 mdl.removeTab (mdl.size() -1); 282 assertEventFired(); 283 assertPravda (mdl.size()-1 == sel.getSelectedIndex(), "After remove of selected final element, selected index should be " + (mdl.size()-1) + "but is " + sel.getSelectedIndex()); 284 285 sel.setSelectedIndex (10); 286 mdl.removeTabs (8, 12); 287 assertEventFired(); 288 assertPravda (sel.getSelectedIndex() == 8, "After contiguous remove straddling selection, selection should be 8 but is " + sel.getSelectedIndex()); 289 290 291 mdl.removeTabs(0, mdl.size()); 292 assertEventFired(); 293 assertPravda (sel.getSelectedIndex() == -1, "After remove of all elements, selected index should be -1 but is " + sel.getSelectedIndex()); 294 295 prepareModel(); 296 sel.setSelectedIndex (10); 297 int[] indices = new int[] {5, 8, 10, 15}; 298 mdl.removeTabs (indices); 299 assertEventFired(); 300 assertPravda (sel.getSelectedIndex() == 8, "After remove two lower elements and the selected element, selected index should be 8 but is " + sel.getSelectedIndex()); 301 302 sel.setSelectedIndex (10); 303 indices = new int[] {5, 8, 3, 2}; 304 mdl.removeTabs (indices); 305 assertEventFired(); 306 assertPravda (sel.getSelectedIndex() == 6, "After noncontiguous remove four lower elements, selected index should be 6 but is " + sel.getSelectedIndex()); 307 308 sel.setSelectedIndex (10); 309 indices = new int[] {12,13,14}; 310 mdl.removeTabs (indices); 311 assertEventFired(); 312 assertPravda (sel.getSelectedIndex() == 10, "After noncontiguous remove of four upper elements, selected index should still be 10 but is " + sel.getSelectedIndex()); 313 314 315 int ids[] = new int[mdl.size()]; 316 for (int j=0; j < ids.length; j++) { 317 ids[j] = j; 318 } 319 mdl.removeTabs (ids); 320 assertEventFired(); 321 assertPravda (sel.getSelectedIndex() == -1, "After noncontiguous remove of all elements, selected index should be -1 but is " + sel.getSelectedIndex()); 322 } 323 324 public void doTestRemoveLastWhenSelected () { 325 System.err.println("testRemoveLastWhenSelected"); 326 prepareModel(); 327 int sz = mdl.size(); 328 System.err.println("Size is " + sz); 329 int i = sz - 1; 330 sel.setSelectedIndex(i); 331 mdl.removeTab(i); 332 System.err.println("Size is now " + mdl.size()); 333 assertEventFired(); 334 assertPravda (sz != mdl.size(), "After removal of one tab, model size has not changed."); 335 assertPravda (mdl.size()-1== sel.getSelectedIndex(), "After removal of the selected index " + i + " (final index in model), selected index should be model.size() -1 (" +(mdl.size()-1) + ") but is " + sel.getSelectedIndex()); 336 } 337 338 339 public void doTestSetIllegalIndex() { 340 System.err.println("testSetIllegalIndex"); 341 Exception e = null; 342 try { 343 sel.setSelectedIndex(mdl.size() + 100); 344 } catch (IllegalArgumentException e1) { 345 e = e1; 346 } 347 assertPravda (e != null, "Set selection to an illegal positive value, but no exception was thrown"); 348 try { 349 sel.setSelectedIndex(0 - mdl.size()); 350 } catch (IllegalArgumentException e1) { 351 e = e1; 352 } 353 assertPravda (e != null, "Set selection to an illegal negative value, but no exception was thrown"); 354 } 355 356 357 public void doTestRemoveMultipleIndices() { 358 System.err.println("testRemoveMultipleIndices"); 359 prepareModel(); 360 sel.setSelectedIndex(mdl.size()-4); 361 int m = mdl.size()-1; 362 int[] toRemove = new int[8]; 363 for (int i=0; i < toRemove.length; i++) { 364 toRemove[i]=m-i; 365 } 366 mdl.removeTabs(toRemove); 367 assertEventFired(); 368 assertPravda (sel.getSelectedIndex() < mdl.size(), "After remove of non-contiguous indices, selected index is " + sel.getSelectedIndex() + " but model only contains " + mdl.size() + " entries."); 369 } 370 373 public static void main(String [] args) { 374 new SelectionModelTest("foo").testEverything(); 375 } 376 377 public void contentsChanged(ListDataEvent e) { 378 lastListenerCall="contentsChanged"; 379 lastEvent = (ComplexListDataEvent)e; 380 if (noEvent) { 381 assertPravda (false, "No event expected but " + e + " receieved"); 382 } 383 } 384 385 public void indicesAdded(ComplexListDataEvent e) { 386 lastListenerCall="indicesAdded"; 387 lastEvent = e; 388 if (noEvent) { 389 assertPravda (false, "No event expected but " + e + " receieved"); 390 } 391 } 392 393 public void indicesChanged(ComplexListDataEvent e) { 394 lastListenerCall="indicesChanged"; 395 lastEvent = e; 396 if (noEvent) { 397 assertPravda (false, "No event expected but " + e + " receieved"); 398 } 399 } 400 401 public void indicesRemoved(ComplexListDataEvent e) { 402 lastListenerCall="indicesRemoved"; 403 lastEvent = e; 404 if (noEvent) { 405 assertPravda (false, "No event expected but " + e + " receieved"); 406 } 407 } 408 409 public void intervalAdded(ListDataEvent e) { 410 lastListenerCall="intervalAdded"; 411 lastEvent = (ComplexListDataEvent)e; 412 if (noEvent) { 413 assertPravda (false, "No event expected but " + e + " receieved"); 414 } 415 } 416 417 public void intervalRemoved(ListDataEvent e) { 418 lastListenerCall="intervalRemoved"; 419 lastEvent = (ComplexListDataEvent)e; 420 if (noEvent) { 421 assertPravda (false, "No event expected but " + e + " receieved"); 422 } 423 } 424 425 public void stateChanged(ChangeEvent e) { 426 eventFired = true; 427 if (noSelEvent) { 428 assertPravda (false, "No change event expected but " + e + " receieved"); 429 } 430 } 431 432 boolean eventFired=false; 433 boolean noEvent = false; 434 boolean noSelEvent=false; 435 } 436 | Popular Tags |