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.Graphics;
046: import javax.swing.Icon;
047: import javax.swing.JLabel;
048: import javax.swing.event.ChangeEvent;
049: import javax.swing.event.ChangeListener;
050: import javax.swing.event.ListDataEvent;
051: import junit.framework.TestCase;
052: import org.netbeans.swing.tabcontrol.DefaultTabDataModel;
053: import org.netbeans.swing.tabcontrol.TabData;
054: import org.netbeans.swing.tabcontrol.TabDataModel;
055: import org.netbeans.swing.tabcontrol.event.ComplexListDataEvent;
056: import org.netbeans.swing.tabcontrol.event.ComplexListDataListener;
057:
058: /** Tests for all of the functionality of TabLayoutModel instances
059: *
060: * @author Tim Boudreau
061: */
062: public class SelectionModelTest extends TestCase implements
063: ComplexListDataListener, ChangeListener {
064: TabDataModel mdl;
065: DefaultTabSelectionModel sel;
066: String lastListenerCall = null;
067: ComplexListDataEvent lastEvent = null;
068:
069: public SelectionModelTest(String testName) {
070: super (testName);
071: }
072:
073: Icon ic = new Icon() {
074: public int getIconWidth() {
075: return 16;
076: }
077:
078: public int getIconHeight() {
079: return 16;
080: }
081:
082: public void paintIcon(Component c, Graphics g, int x, int y) {
083: //do nothing
084: }
085: };
086:
087: Icon sameSizeIcon = new Icon() {
088: public int getIconWidth() {
089: return 16;
090: }
091:
092: public int getIconHeight() {
093: return 16;
094: }
095:
096: public void paintIcon(Component c, Graphics g, int x, int y) {
097: //do nothing
098: }
099: };
100:
101: Icon biggerIcon = new Icon() {
102: public int getIconWidth() {
103: return 22;
104: }
105:
106: public int getIconHeight() {
107: return 22;
108: }
109:
110: public void paintIcon(Component c, Graphics g, int x, int y) {
111: //do nothing
112: }
113: };
114:
115: /** Creates a new instance of SelectionModelTest */
116: public void setUp() {
117: prepareModel();
118: }
119:
120: /** Weird, but this class was adapted from a standalone test written
121: * long ago and rescued from cvs history. It didn't use JUnit, and
122: * the assertTrue argument order was reversed. So in the interest of
123: * laziness... */
124: private void assertPravda(boolean val, String msg) {
125: assertTrue(msg, val);
126: }
127:
128: private void assertEventFired() {
129: assertPravda(eventFired, "Event expected but none fired");
130: eventFired = false;
131: }
132:
133: private void prepareModel() {
134: if (mdl != null) {
135: mdl.removeComplexListDataListener(this );
136: }
137: TabData[] td = new TabData[25];
138: int ct = 0;
139: for (char c = 'a'; c < 'z'; c++) {
140: String name = new String(new char[] { c });
141: Component comp = new JLabel(name);
142: comp.setName(name);
143: td[ct] = new TabData(comp, ic, name, "tip:" + name);
144: ct++;
145: }
146: mdl = new DefaultTabDataModel(td);
147: // mdl.addComplexListDataListener(this);
148: sel = new DefaultTabSelectionModel(mdl);
149: sel.addChangeListener(this );
150: }
151:
152: /** Due to the non-junit way this test was originally written, tests must
153: * be run in a specific order, since each makes modifiecations to the model
154: * that the next one depends on */
155: public void testEverything() {
156: doTestGetSelectedIndex();
157: doTestSetSelectedIndex();
158: doTestInsertSingle();
159: doTestRemoveSingle();
160: doTestInsertContiguous();
161: //XXX fix these older tests later
162: /* doTestRemoveContiguous();
163: doTestInsertNonContiguous();
164: doTestRemoveNonContiguous();
165: doTestSet();
166: doTestSetIllegalIndex();
167: doTestRemoveSelected();
168: doTestRemoveLastWhenSelected();
169: doTestRemoveMultipleIndices();
170: */
171: System.err.println("All tests passed for selection model");
172: }
173:
174: public void doTestGetSelectedIndex() {
175: System.err.println("testGetSelectedIndex");
176: int i = sel.getSelectedIndex();
177: assertPravda(i == -1, "Initial selected index should be -1");
178: }
179:
180: public void doTestSetSelectedIndex() {
181: System.err.println("testSetSelectedIndex");
182: eventFired = false;
183: sel.setSelectedIndex(5);
184: assertEventFired();
185: assertPravda(5 == sel.getSelectedIndex(),
186: "Selected index should be 5 but is "
187: + sel.getSelectedIndex());
188: //make sure no event fired if the selection is the same
189: noEvent = true;
190: sel.setSelectedIndex(5);
191: noEvent = false;
192: }
193:
194: public void doTestInsertSingle() {
195: System.err.println("testInsertSingle");
196: TabData td = new TabData(new JLabel(), ic, "insertData",
197: "Tip:insertData");
198: noEvent = true;
199: mdl.addTab(7, td);
200: noEvent = false;
201: assertPravda(5 == sel.getSelectedIndex(),
202: "Selected index should be 5 but is "
203: + sel.getSelectedIndex());
204: TabData tdb = new TabData(new JLabel(), ic, "insertData2",
205: "Tip:insertData2");
206: mdl.addTab(3, tdb);
207: assertEventFired();
208: assertPravda(6 == sel.getSelectedIndex(),
209: "After insert, selected index should be 6 but is "
210: + sel.getSelectedIndex());
211: }
212:
213: public void doTestRemoveSingle() {
214: System.err.println("testRemoveSingle");
215: mdl.removeTab(3);
216: assertEventFired();
217: assertPravda(5 == sel.getSelectedIndex(),
218: "After remove, selected index should be 5 but is "
219: + sel.getSelectedIndex());
220: noEvent = true;
221: mdl.removeTab(7);
222: noEvent = false;
223: assertPravda(5 == sel.getSelectedIndex(),
224: "After remove, selected index should be 5 but is "
225: + sel.getSelectedIndex());
226: //model state is original state now
227: }
228:
229: public void doTestInsertContiguous() {
230: System.err.println("testInsertContiguous");
231:
232: TabData[] td = new TabData[5];
233: for (int i = 0; i < 5; i++) {
234: String nm = "ic" + Integer.toString(i);
235: td[i] = new TabData(new JLabel(), ic, nm, "tip:nm");
236: }
237:
238: int idx = sel.getSelectedIndex();
239: mdl.addTabs(0, td);
240: assertEventFired();
241: assertPravda(idx + 4 == sel.getSelectedIndex(),
242: "After contiguous insert, selection should be "
243: + (idx + 5) + " but is "
244: + sel.getSelectedIndex());
245: noEvent = true;
246: mdl.addTabs(20, td);
247: noEvent = false;
248: }
249:
250: public void doTestRemoveContiguous() {
251: System.err.println("testRemoveContiguous");
252: noEvent = true;
253: mdl.removeTabs(20, 24);
254: noEvent = false;
255: mdl.removeTabs(2, 6);
256: assertEventFired();
257: assertPravda(4 == sel.getSelectedIndex(),
258: "After contiguous remove, selected index should be 5 but is "
259: + sel.getSelectedIndex());
260: }
261:
262: public void doTestInsertNonContiguous() {
263: System.err.println("testInsertNonContiguous");
264: sel.setSelectedIndex(10);
265: int[] indices = { 1, 3, 5, 7 };
266: TabData[] td = new TabData[4];
267: for (int i = 0; i < 4; i++) {
268: String nm = "icnc" + Integer.toString(i);
269: td[i] = new TabData(new JLabel(), ic, nm, "tip:nm");
270: }
271: mdl.addTabs(indices, td);
272: assertEventFired();
273: assertPravda(14 == sel.getSelectedIndex(),
274: "After non contiguous add of 4 items, selected index should be 14 but is "
275: + sel.getSelectedIndex());
276:
277: indices = new int[] { 16, 17, 21, 23 };
278: noEvent = true;
279: mdl.addTabs(indices, td);
280: noEvent = false;
281: assertPravda(
282: 14 == sel.getSelectedIndex(),
283: "After non contiguous add above the selected index, selected index should still be 14 but is "
284: + sel.getSelectedIndex());
285:
286: indices = new int[] { 3, 5, 22, 23 };
287: mdl.addTabs(indices, td);
288: assertEventFired();
289: assertPravda(
290: 16 == sel.getSelectedIndex(),
291: "After non contiguous add straddling the selected index, selected index should still be 16 but is "
292: + sel.getSelectedIndex());
293: }
294:
295: public void doTestRemoveNonContiguous() {
296: System.err.println("testRemoveNonContiguous");
297: sel.setSelectedIndex(10);
298: int[] indices = { 1, 3, 5, 7 };
299: mdl.removeTabs(indices);
300: assertEventFired();
301: assertPravda(
302: 5 == sel.getSelectedIndex(),
303: "After non contiguous remove before the selected index, selected index should still be 6 but is "
304: + sel.getSelectedIndex());
305:
306: indices = new int[] { 13, 15, 17 };
307: noEvent = true;
308: mdl.removeTabs(indices);
309: noEvent = false;
310: assertPravda(
311: 5 == sel.getSelectedIndex(),
312: "After non contiguous remove above the selected index, selected index should still be 6 but is "
313: + sel.getSelectedIndex());
314:
315: indices = new int[] { 2, 5, 18, 19 };
316: mdl.removeTabs(indices);
317: assertEventFired();
318: assertPravda(
319: 4 == sel.getSelectedIndex(),
320: "After non contiguous remove before the selected index, selected index should still be 6 but is "
321: + sel.getSelectedIndex());
322: }
323:
324: public void doTestSet() {
325: System.err.println("testSet");
326: int i = sel.getSelectedIndex();
327: TabData td = new TabData(new JLabel(), ic, "inserted",
328: "tip:inserted");
329: noEvent = true;
330: mdl.setTab(i, td);
331: mdl.setTab(i - 3, td);
332: mdl.setTab(i + 5, td);
333: noEvent = false;
334: }
335:
336: public void doTestRemoveSelected() {
337: System.err.println("testRemoveSelected");
338: prepareModel();
339: int i = mdl.size() - 5;
340: sel.setSelectedIndex(i);
341: mdl.removeTab(i);
342: assertEventFired();
343: assertPravda(
344: i == sel.getSelectedIndex(),
345: "After remove of the selected index when not at edge, selected index should still be "
346: + i + " but is " + sel.getSelectedIndex());
347:
348: sel.setSelectedIndex(mdl.size() - 1);
349: mdl.removeTab(mdl.size() - 1);
350: assertEventFired();
351: assertPravda(mdl.size() - 1 == sel.getSelectedIndex(),
352: "After remove of selected final element, selected index should be "
353: + (mdl.size() - 1) + "but is "
354: + sel.getSelectedIndex());
355:
356: sel.setSelectedIndex(10);
357: mdl.removeTabs(8, 12);
358: assertEventFired();
359: assertPravda(sel.getSelectedIndex() == 8,
360: "After contiguous remove straddling selection, selection should be 8 but is "
361: + sel.getSelectedIndex());
362:
363: mdl.removeTabs(0, mdl.size());
364: assertEventFired();
365: assertPravda(sel.getSelectedIndex() == -1,
366: "After remove of all elements, selected index should be -1 but is "
367: + sel.getSelectedIndex());
368:
369: prepareModel();
370: sel.setSelectedIndex(10);
371: int[] indices = new int[] { 5, 8, 10, 15 };
372: mdl.removeTabs(indices);
373: assertEventFired();
374: assertPravda(
375: sel.getSelectedIndex() == 8,
376: "After remove two lower elements and the selected element, selected index should be 8 but is "
377: + sel.getSelectedIndex());
378:
379: sel.setSelectedIndex(10);
380: indices = new int[] { 5, 8, 3, 2 };
381: mdl.removeTabs(indices);
382: assertEventFired();
383: assertPravda(
384: sel.getSelectedIndex() == 6,
385: "After noncontiguous remove four lower elements, selected index should be 6 but is "
386: + sel.getSelectedIndex());
387:
388: sel.setSelectedIndex(10);
389: indices = new int[] { 12, 13, 14 };
390: mdl.removeTabs(indices);
391: assertEventFired();
392: assertPravda(
393: sel.getSelectedIndex() == 10,
394: "After noncontiguous remove of four upper elements, selected index should still be 10 but is "
395: + sel.getSelectedIndex());
396:
397: int ids[] = new int[mdl.size()];
398: for (int j = 0; j < ids.length; j++) {
399: ids[j] = j;
400: }
401: mdl.removeTabs(ids);
402: assertEventFired();
403: assertPravda(
404: sel.getSelectedIndex() == -1,
405: "After noncontiguous remove of all elements, selected index should be -1 but is "
406: + sel.getSelectedIndex());
407: }
408:
409: public void doTestRemoveLastWhenSelected() {
410: System.err.println("testRemoveLastWhenSelected");
411: prepareModel();
412: int sz = mdl.size();
413: System.err.println("Size is " + sz);
414: int i = sz - 1;
415: sel.setSelectedIndex(i);
416: mdl.removeTab(i);
417: System.err.println("Size is now " + mdl.size());
418: assertEventFired();
419: assertPravda(sz != mdl.size(),
420: "After removal of one tab, model size has not changed.");
421: assertPravda(
422: mdl.size() - 1 == sel.getSelectedIndex(),
423: "After removal of the selected index "
424: + i
425: + " (final index in model), selected index should be model.size() -1 ("
426: + (mdl.size() - 1) + ") but is "
427: + sel.getSelectedIndex());
428: }
429:
430: public void doTestSetIllegalIndex() {
431: System.err.println("testSetIllegalIndex");
432: Exception e = null;
433: try {
434: sel.setSelectedIndex(mdl.size() + 100);
435: } catch (IllegalArgumentException e1) {
436: e = e1;
437: }
438: assertPravda(e != null,
439: "Set selection to an illegal positive value, but no exception was thrown");
440: try {
441: sel.setSelectedIndex(0 - mdl.size());
442: } catch (IllegalArgumentException e1) {
443: e = e1;
444: }
445: assertPravda(e != null,
446: "Set selection to an illegal negative value, but no exception was thrown");
447: }
448:
449: public void doTestRemoveMultipleIndices() {
450: System.err.println("testRemoveMultipleIndices");
451: prepareModel();
452: sel.setSelectedIndex(mdl.size() - 4);
453: int m = mdl.size() - 1;
454: int[] toRemove = new int[8];
455: for (int i = 0; i < toRemove.length; i++) {
456: toRemove[i] = m - i;
457: }
458: mdl.removeTabs(toRemove);
459: assertEventFired();
460: assertPravda(sel.getSelectedIndex() < mdl.size(),
461: "After remove of non-contiguous indices, selected index is "
462: + sel.getSelectedIndex()
463: + " but model only contains " + mdl.size()
464: + " entries.");
465: }
466:
467: /**
468: * @param args the command line arguments
469: */
470: public static void main(String[] args) {
471: new SelectionModelTest("foo").testEverything();
472: }
473:
474: public void contentsChanged(ListDataEvent e) {
475: lastListenerCall = "contentsChanged";
476: lastEvent = (ComplexListDataEvent) e;
477: if (noEvent) {
478: assertPravda(false, "No event expected but " + e
479: + " receieved");
480: }
481: }
482:
483: public void indicesAdded(ComplexListDataEvent e) {
484: lastListenerCall = "indicesAdded";
485: lastEvent = e;
486: if (noEvent) {
487: assertPravda(false, "No event expected but " + e
488: + " receieved");
489: }
490: }
491:
492: public void indicesChanged(ComplexListDataEvent e) {
493: lastListenerCall = "indicesChanged";
494: lastEvent = e;
495: if (noEvent) {
496: assertPravda(false, "No event expected but " + e
497: + " receieved");
498: }
499: }
500:
501: public void indicesRemoved(ComplexListDataEvent e) {
502: lastListenerCall = "indicesRemoved";
503: lastEvent = e;
504: if (noEvent) {
505: assertPravda(false, "No event expected but " + e
506: + " receieved");
507: }
508: }
509:
510: public void intervalAdded(ListDataEvent e) {
511: lastListenerCall = "intervalAdded";
512: lastEvent = (ComplexListDataEvent) e;
513: if (noEvent) {
514: assertPravda(false, "No event expected but " + e
515: + " receieved");
516: }
517: }
518:
519: public void intervalRemoved(ListDataEvent e) {
520: lastListenerCall = "intervalRemoved";
521: lastEvent = (ComplexListDataEvent) e;
522: if (noEvent) {
523: assertPravda(false, "No event expected but " + e
524: + " receieved");
525: }
526: }
527:
528: public void stateChanged(ChangeEvent e) {
529: eventFired = true;
530: if (noSelEvent) {
531: assertPravda(false, "No change event expected but " + e
532: + " receieved");
533: }
534: }
535:
536: boolean eventFired = false;
537: boolean noEvent = false;
538: boolean noSelEvent = false;
539: }
|