001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Anton Avtamonov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.event.KeyListener;
023: import java.util.ArrayList;
024: import java.util.List;
025: import javax.swing.event.ListSelectionEvent;
026: import javax.swing.event.ListSelectionListener;
027:
028: public class DefaultListSelectionModelTest extends SwingTestCase {
029: private DefaultListSelectionModel model;
030:
031: private TestListener listener;
032:
033: public DefaultListSelectionModelTest(final String name) {
034: super (name);
035: }
036:
037: @Override
038: protected void setUp() throws Exception {
039: model = new DefaultListSelectionModel();
040: listener = new TestListener();
041: }
042:
043: @Override
044: protected void tearDown() throws Exception {
045: model = null;
046: listener = null;
047: }
048:
049: public void testAddRemoveListSelectionListener() throws Exception {
050: assertEquals(0, model.getListSelectionListeners().length);
051: model.addListSelectionListener(new TestListener());
052: model.addListSelectionListener(listener);
053: model.addListSelectionListener(new TestListener());
054: assertEquals(3, model.getListSelectionListeners().length);
055: model.removeListSelectionListener(listener);
056: assertEquals(2, model.getListSelectionListeners().length);
057: }
058:
059: public void testAddSelectionInterval() throws Exception {
060: model.addListSelectionListener(listener);
061: model.addSelectionInterval(-1, 0);
062: model.addSelectionInterval(-1, 10);
063: model.addSelectionInterval(0, -1);
064: model.addSelectionInterval(-1, -1);
065: assertTrue(model.isSelectionEmpty());
066: assertEquals(0, listener.getEvents().size());
067: listener.reset();
068: model.setLeadAnchorNotificationEnabled(false);
069: assertTrue(model.isSelectionEmpty());
070: model.addSelectionInterval(3, 5);
071: checkSingleEvent(3, 5, false);
072: assertFalse(model.isSelectedIndex(2));
073: checkIntervalState(3, 5, true);
074: listener.reset();
075: model.addSelectionInterval(10, 7);
076: checkSingleEvent(7, 10, false);
077: checkIntervalState(3, 5, true);
078: checkIntervalState(7, 10, true);
079: listener.reset();
080: model.addSelectionInterval(4, 11);
081: checkSingleEvent(6, 11, false);
082: checkIntervalState(3, 11, true);
083: model.clearSelection();
084: model
085: .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
086: model.addSelectionInterval(4, 11);
087: checkIntervalState(4, 11, true);
088: model.addSelectionInterval(6, 8);
089: checkIntervalState(6, 8, true);
090: model.clearSelection();
091: model.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
092: model.addSelectionInterval(11, 4);
093: checkIntervalState(4, 4, true);
094: assertEquals(model.getMaxSelectionIndex(), model
095: .getMinSelectionIndex());
096: }
097:
098: public void testClearSelectionIsSelectionEmpty() throws Exception {
099: assertTrue(model.isSelectionEmpty());
100: model.addSelectionInterval(0, 5);
101: model.addSelectionInterval(7, 9);
102: assertFalse(model.isSelectionEmpty());
103: model.addListSelectionListener(listener);
104: model.clearSelection();
105: checkSingleEvent(0, 9, false);
106: assertTrue(model.isSelectionEmpty());
107: checkIntervalState(0, 9, false);
108: }
109:
110: public void testClone() throws Exception {
111: model.addListSelectionListener(listener);
112: model.addSelectionInterval(2, 4);
113: model.addSelectionInterval(7, 9);
114: model.setLeadAnchorNotificationEnabled(false);
115: model.setValueIsAdjusting(true);
116: model = (DefaultListSelectionModel) model.clone();
117: assertEquals(0, model.getListSelectionListeners().length);
118: checkIntervalState(0, 1, false);
119: checkIntervalState(2, 4, true);
120: checkIntervalState(5, 6, false);
121: checkIntervalState(7, 9, true);
122: assertEquals(7, model.getAnchorSelectionIndex());
123: assertEquals(9, model.getLeadSelectionIndex());
124: assertFalse(model.isLeadAnchorNotificationEnabled());
125: assertTrue(model.getValueIsAdjusting());
126: }
127:
128: public void testFireValueChanged() throws Exception {
129: TestListener listener2 = new TestListener();
130: model.addListSelectionListener(listener);
131: model.addListSelectionListener(listener2);
132: model.fireValueChanged(true);
133: assertEquals(0, listener.getEvents().size());
134: listener.reset();
135: model.setValueIsAdjusting(true);
136: model.setAnchorSelectionIndex(5);
137: checkSingleEvent(listener, 5, 5, true);
138: listener.reset();
139: model.fireValueChanged(true);
140: checkSingleEvent(listener, 5, 5, true);
141: model.setSelectionInterval(3, 6);
142: model.setSelectionInterval(9, 11);
143: listener.reset();
144: model.fireValueChanged(true);
145: checkSingleEvent(listener, 3, 11, true);
146: listener.reset();
147: model.fireValueChanged(false);
148: assertEquals(0, listener.getEvents().size());
149: model.setValueIsAdjusting(false);
150: listener.reset();
151: listener2.reset();
152: model.fireValueChanged(3, 7);
153: checkSingleEvent(listener, 3, 7, false);
154: checkSingleEvent(listener2, 3, 7, false);
155: listener.reset();
156: listener2.reset();
157: model.fireValueChanged(0, 5, false);
158: checkSingleEvent(listener, 0, 5, false);
159: checkSingleEvent(listener2, 0, 5, false);
160: }
161:
162: public void testGetAnchorAndLeadSelectionIndex() throws Exception {
163: assertEquals(-1, model.getAnchorSelectionIndex());
164: assertEquals(-1, model.getLeadSelectionIndex());
165: model.addSelectionInterval(2, 6);
166: assertEquals(2, model.getAnchorSelectionIndex());
167: assertEquals(6, model.getLeadSelectionIndex());
168: model.setSelectionInterval(1, 4);
169: assertEquals(1, model.getAnchorSelectionIndex());
170: assertEquals(4, model.getLeadSelectionIndex());
171: model.removeSelectionInterval(2, 7);
172: assertEquals(2, model.getAnchorSelectionIndex());
173: assertEquals(7, model.getLeadSelectionIndex());
174: }
175:
176: public void testGetListeners() throws Exception {
177: assertEquals(0,
178: model.getListeners(ListSelectionListener.class).length);
179: assertEquals(0, model.getListeners(KeyListener.class).length);
180: model.addListSelectionListener(listener);
181: assertEquals(1,
182: model.getListeners(ListSelectionListener.class).length);
183: assertEquals(0, model.getListeners(KeyListener.class).length);
184: }
185:
186: public void testGetListSelectionListeners() throws Exception {
187: assertEquals(0, model.getListSelectionListeners().length);
188: model.addListSelectionListener(listener);
189: assertEquals(1, model.getListSelectionListeners().length);
190: }
191:
192: public void testGetMinAndMaxSelectionIndex() throws Exception {
193: assertEquals(-1, model.getMinSelectionIndex());
194: assertEquals(-1, model.getMaxSelectionIndex());
195: model.addSelectionInterval(2, 6);
196: model.addSelectionInterval(12, 9);
197: assertEquals(2, model.getMinSelectionIndex());
198: assertEquals(12, model.getMaxSelectionIndex());
199: model.addSelectionInterval(0, 14);
200: assertEquals(0, model.getMinSelectionIndex());
201: assertEquals(14, model.getMaxSelectionIndex());
202: }
203:
204: public void testGetSetSelectionMode() throws Exception {
205: assertEquals(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION,
206: model.getSelectionMode());
207: model.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
208: assertEquals(ListSelectionModel.SINGLE_SELECTION, model
209: .getSelectionMode());
210: model
211: .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
212: assertEquals(ListSelectionModel.SINGLE_INTERVAL_SELECTION,
213: model.getSelectionMode());
214: try {
215: model.setSelectionMode(100);
216: fail("Incorrect selection model should be detected");
217: } catch (IllegalArgumentException iae) {
218: }
219: }
220:
221: public void testGetSetValueIsAdjusting() throws Exception {
222: model.addListSelectionListener(listener);
223: assertFalse(model.getValueIsAdjusting());
224: model.setValueIsAdjusting(true);
225: assertEquals(0, listener.getEvents().size());
226: assertTrue(model.getValueIsAdjusting());
227: model.setSelectionInterval(2, 3);
228: checkSingleEvent(2, 3, true);
229: listener.reset();
230: model.setSelectionInterval(5, 7);
231: checkSingleEvent(2, 7, true);
232: listener.reset();
233: model.setSelectionInterval(5, 8);
234: checkSingleEvent(7, 8, true);
235: listener.reset();
236: model.setValueIsAdjusting(false);
237: assertEquals(1, listener.getEvents().size());
238: assertFalse(model.getValueIsAdjusting());
239: checkSingleEvent(2, 8, false);
240: }
241:
242: public void testInsertIndexInterval() throws Exception {
243: model.addListSelectionListener(listener);
244: model.setSelectionInterval(3, 5);
245: if (isHarmony()) {
246: listener.reset();
247: model.insertIndexInterval(-1, 0, true);
248: model.insertIndexInterval(-1, 0, false);
249: model.insertIndexInterval(-1, -1, true);
250: model.insertIndexInterval(-1, -1, false);
251: model.insertIndexInterval(0, -1, true);
252: model.insertIndexInterval(0, -1, false);
253: assertEquals(0, listener.getEvents().size());
254: }
255: listener.reset();
256: model.insertIndexInterval(4, 10, true);
257: checkIntervalState(0, 2, false);
258: checkIntervalState(3, 15, true);
259: if (isHarmony()) {
260: checkSingleEvent(5, 15, false);
261: }
262: listener.reset();
263: model.insertIndexInterval(0, 3, true);
264: checkIntervalState(0, 5, false);
265: checkIntervalState(6, 18, true);
266: checkSingleEvent(3, 18, false);
267: model.clearSelection();
268: model.setSelectionInterval(3, 5);
269: listener.reset();
270: model.insertIndexInterval(6, 3, true);
271: checkIntervalState(0, 2, false);
272: checkIntervalState(3, 5, true);
273: checkIntervalState(6, 20, false);
274: assertEquals(0, listener.getEvents().size());
275: assertEquals(3, model.getAnchorSelectionIndex());
276: assertEquals(5, model.getLeadSelectionIndex());
277: model.clearSelection();
278: model.setSelectionInterval(3, 5);
279: listener.reset();
280: model.insertIndexInterval(6, 3, false);
281: checkIntervalState(0, 2, false);
282: checkIntervalState(3, 5, true);
283: checkIntervalState(6, 20, false);
284: assertEquals(0, listener.getEvents().size());
285: assertEquals(3, model.getAnchorSelectionIndex());
286: assertEquals(5, model.getLeadSelectionIndex());
287: model.clearSelection();
288: model.setSelectionInterval(3, 5);
289: listener.reset();
290: model.insertIndexInterval(5, 3, false);
291: checkIntervalState(0, 2, false);
292: checkIntervalState(3, 8, true);
293: checkIntervalState(9, 20, false);
294: checkSingleEvent(6, 8, false);
295: assertEquals(3, model.getAnchorSelectionIndex());
296: assertEquals(5, model.getLeadSelectionIndex());
297: model.clearSelection();
298: model.setSelectionInterval(3, 5);
299: listener.reset();
300: model.insertIndexInterval(5, 3, true);
301: checkIntervalState(0, 2, false);
302: checkIntervalState(3, 8, true);
303: checkIntervalState(9, 20, false);
304: checkSingleEvent(5, 8, false);
305: assertEquals(3, model.getAnchorSelectionIndex());
306: assertEquals(8, model.getLeadSelectionIndex());
307: model.clearSelection();
308: model.setSelectionInterval(3, 5);
309: listener.reset();
310: model.insertIndexInterval(3, 3, true);
311: checkIntervalState(0, 2, false);
312: checkIntervalState(3, 8, true);
313: checkIntervalState(9, 20, false);
314: checkSingleEvent(3, 8, false);
315: assertEquals(6, model.getAnchorSelectionIndex());
316: assertEquals(8, model.getLeadSelectionIndex());
317: model.clearSelection();
318: model.setSelectionInterval(1, 2);
319: listener.reset();
320: model.insertIndexInterval(0, 3, true);
321: checkIntervalState(0, 3, false);
322: checkIntervalState(4, 5, true);
323: checkIntervalState(6, 20, false);
324: assertEquals(4, model.getAnchorSelectionIndex());
325: assertEquals(5, model.getLeadSelectionIndex());
326: listener.reset();
327: model.removeSelectionInterval(-1, 0);
328: model.removeSelectionInterval(0, -1);
329: model.removeSelectionInterval(-1, -1);
330: assertEquals(0, listener.getEvents().size());
331: }
332:
333: public void testRemoveIndexInterval() throws Exception {
334: model.setSelectionInterval(3, 8);
335: model.addListSelectionListener(listener);
336: model.removeSelectionInterval(-1, 10);
337: model.removeSelectionInterval(-1, 0);
338: model.removeSelectionInterval(0, -1);
339: model.removeSelectionInterval(-1, -1);
340: assertEquals(0, listener.getEvents().size());
341: assertEquals(3, model.getAnchorSelectionIndex());
342: assertEquals(8, model.getLeadSelectionIndex());
343: listener.reset();
344: model.removeIndexInterval(2, 6);
345: checkIntervalState(0, 1, false);
346: checkIntervalState(2, 3, true);
347: assertEquals(1, model.getAnchorSelectionIndex());
348: assertEquals(3, model.getLeadSelectionIndex());
349: checkSingleEvent(1, 8, false);
350: listener.reset();
351: model.removeIndexInterval(0, 2);
352: checkIntervalState(0, 0, true);
353: checkIntervalState(1, 10, false);
354: assertEquals(-1, model.getAnchorSelectionIndex());
355: assertEquals(0, model.getLeadSelectionIndex());
356: if (isHarmony()) {
357: checkSingleEvent(0, 3, false);
358: } else {
359: checkSingleEvent(-1, 3, false);
360: }
361: listener.reset();
362: model.removeIndexInterval(0, 2);
363: checkIntervalState(0, 10, false);
364: assertEquals(-1, model.getAnchorSelectionIndex());
365: if (isHarmony()) {
366: assertEquals(-1, model.getLeadSelectionIndex());
367: }
368: checkSingleEvent(0, 0, false);
369: model.setSelectionInterval(3, 8);
370: listener.reset();
371: model.removeIndexInterval(8, 8);
372: checkIntervalState(3, 7, true);
373: checkIntervalState(8, 8, false);
374: assertEquals(3, model.getAnchorSelectionIndex());
375: assertEquals(7, model.getLeadSelectionIndex());
376: checkSingleEvent(7, 8, false);
377: listener.reset();
378: model.removeIndexInterval(3, 3);
379: checkIntervalState(3, 6, true);
380: checkIntervalState(7, 8, false);
381: assertEquals(2, model.getAnchorSelectionIndex());
382: assertEquals(6, model.getLeadSelectionIndex());
383: checkSingleEvent(2, 7, false);
384: listener.reset();
385: model.removeIndexInterval(3, 6);
386: checkIntervalState(0, 10, false);
387: assertEquals(2, model.getAnchorSelectionIndex());
388: assertEquals(2, model.getLeadSelectionIndex());
389: checkSingleEvent(2, 6, false);
390: }
391:
392: public void testIsLeadAnchorNotificationEnabled() throws Exception {
393: model.addListSelectionListener(listener);
394: assertTrue(model.isLeadAnchorNotificationEnabled());
395: model.addSelectionInterval(3, 5);
396: checkSingleEvent(3, 5, false);
397: listener.reset();
398: model.addSelectionInterval(7, 8);
399: checkSingleEvent(3, 8, false);
400: listener.reset();
401: model.setSelectionInterval(2, 6);
402: checkSingleEvent(2, 8, false);
403: listener.reset();
404: model.removeSelectionInterval(4, 11);
405: checkSingleEvent(2, 11, false);
406: listener.reset();
407: model.removeSelectionInterval(4, 11);
408: assertEquals(0, listener.getEvents().size());
409: listener.reset();
410: model.removeSelectionInterval(5, 8);
411: checkSingleEvent(4, 11, false);
412: model.setLeadAnchorNotificationEnabled(false);
413: assertFalse(model.isLeadAnchorNotificationEnabled());
414: listener.reset();
415: model.addSelectionInterval(10, 12);
416: checkSingleEvent(10, 12, false);
417: listener.reset();
418: model.removeSelectionInterval(0, 2);
419: checkSingleEvent(2, 2, false);
420: listener.reset();
421: model.removeSelectionInterval(1, 2);
422: assertEquals(0, listener.getEvents().size());
423: }
424:
425: public void testIsSelectedIndex() throws Exception {
426: model.setSelectionInterval(2, 4);
427: assertFalse(model.isSelectedIndex(0));
428: assertTrue(model.isSelectedIndex(2));
429: assertFalse(model.isSelectedIndex(-1));
430: }
431:
432: public void testGetSetAnchorSelectionIndex() throws Exception {
433: model.addListSelectionListener(listener);
434: model.setAnchorSelectionIndex(3);
435: assertEquals(3, model.getAnchorSelectionIndex());
436: checkSingleEvent(3, 3, false);
437: listener.reset();
438: model.setAnchorSelectionIndex(5);
439: assertEquals(5, model.getAnchorSelectionIndex());
440: checkSingleEvent(3, 5, false);
441: listener.reset();
442: model.setLeadAnchorNotificationEnabled(false);
443: model.setAnchorSelectionIndex(7);
444: assertEquals(7, model.getAnchorSelectionIndex());
445: assertEquals(0, listener.getEvents().size());
446: listener.reset();
447: model.setAnchorSelectionIndex(-1);
448: assertEquals(-1, model.getAnchorSelectionIndex());
449: assertEquals(0, listener.getEvents().size());
450: }
451:
452: public void testGetSetLeadSelectionIndex() throws Exception {
453: model.addListSelectionListener(listener);
454: model.setSelectionInterval(3, 6);
455: listener.reset();
456: model.setLeadSelectionIndex(-1);
457: assertEquals(6, model.getLeadSelectionIndex());
458: assertEquals(0, listener.getEvents().size());
459: listener.reset();
460: model.setLeadSelectionIndex(4);
461: assertEquals(4, model.getLeadSelectionIndex());
462: checkIntervalState(3, 4, true);
463: checkIntervalState(5, 6, false);
464: checkSingleEvent(4, 6, false);
465: model.setSelectionInterval(3, 6);
466: model.setAnchorSelectionIndex(2);
467: listener.reset();
468: model.setLeadSelectionIndex(8);
469: assertEquals(8, model.getLeadSelectionIndex());
470: checkIntervalState(2, 8, false);
471: checkSingleEvent(3, 8, false);
472: assertEquals(2, model.getAnchorSelectionIndex());
473: assertEquals(8, model.getLeadSelectionIndex());
474: model.clearSelection();
475: assertEquals(2, model.getAnchorSelectionIndex());
476: assertEquals(8, model.getLeadSelectionIndex());
477: assertTrue(model.isSelectionEmpty());
478: model.setAnchorSelectionIndex(5);
479: assertEquals(5, model.getAnchorSelectionIndex());
480: assertEquals(8, model.getLeadSelectionIndex());
481: assertTrue(model.isSelectionEmpty());
482: model.setLeadSelectionIndex(8);
483: assertEquals(5, model.getAnchorSelectionIndex());
484: assertEquals(8, model.getLeadSelectionIndex());
485: assertTrue(model.isSelectionEmpty());
486: model.setLeadSelectionIndex(20);
487: assertEquals(5, model.getAnchorSelectionIndex());
488: assertEquals(20, model.getLeadSelectionIndex());
489: assertTrue(model.isSelectionEmpty());
490: model.setAnchorSelectionIndex(1);
491: assertEquals(1, model.getAnchorSelectionIndex());
492: assertEquals(20, model.getLeadSelectionIndex());
493: assertTrue(model.isSelectionEmpty());
494: model.setLeadSelectionIndex(19);
495: assertEquals(1, model.getAnchorSelectionIndex());
496: assertEquals(19, model.getLeadSelectionIndex());
497: checkIntervalState(0, 19, false);
498: checkIntervalState(20, 20, true);
499: checkIntervalState(21, 100, false);
500: model.setSelectionInterval(2, 5);
501: assertEquals(2, model.getAnchorSelectionIndex());
502: assertEquals(5, model.getLeadSelectionIndex());
503: checkIntervalState(0, 1, false);
504: checkIntervalState(2, 5, true);
505: checkIntervalState(6, 10, false);
506: model.setAnchorSelectionIndex(1);
507: assertEquals(1, model.getAnchorSelectionIndex());
508: assertEquals(5, model.getLeadSelectionIndex());
509: checkIntervalState(0, 1, false);
510: checkIntervalState(2, 5, true);
511: checkIntervalState(6, 10, false);
512: model.setLeadSelectionIndex(7);
513: assertEquals(1, model.getAnchorSelectionIndex());
514: assertEquals(7, model.getLeadSelectionIndex());
515: checkIntervalState(0, 10, false);
516: model.setSelectionInterval(2, 7);
517: assertEquals(2, model.getAnchorSelectionIndex());
518: assertEquals(7, model.getLeadSelectionIndex());
519: model.setAnchorSelectionIndex(4);
520: model.setLeadSelectionIndex(6);
521: checkIntervalState(0, 1, false);
522: checkIntervalState(2, 6, true);
523: checkIntervalState(7, 10, false);
524: model.setSelectionInterval(3, 7);
525: assertEquals(3, model.getAnchorSelectionIndex());
526: assertEquals(7, model.getLeadSelectionIndex());
527: model.setLeadSelectionIndex(3);
528: checkIntervalState(0, 2, false);
529: checkIntervalState(3, 3, true);
530: checkIntervalState(4, 10, false);
531: model.setSelectionInterval(3, 7);
532: assertEquals(3, model.getAnchorSelectionIndex());
533: assertEquals(7, model.getLeadSelectionIndex());
534: model.setLeadSelectionIndex(3);
535: checkIntervalState(0, 2, false);
536: checkIntervalState(3, 3, true);
537: checkIntervalState(4, 10, false);
538: model.setSelectionInterval(3, 7);
539: assertEquals(3, model.getAnchorSelectionIndex());
540: assertEquals(7, model.getLeadSelectionIndex());
541: model.setLeadSelectionIndex(1);
542: checkIntervalState(0, 0, false);
543: checkIntervalState(1, 3, true);
544: checkIntervalState(4, 10, false);
545: model.setSelectionInterval(3, 7);
546: assertEquals(3, model.getAnchorSelectionIndex());
547: assertEquals(7, model.getLeadSelectionIndex());
548: model.setAnchorSelectionIndex(5);
549: model.setLeadSelectionIndex(1);
550: checkIntervalState(0, 0, false);
551: checkIntervalState(1, 5, true);
552: checkIntervalState(6, 10, false);
553: }
554:
555: public void testMoveLeadSelectionIndex() throws Exception {
556: model.addListSelectionListener(listener);
557: model.setSelectionInterval(3, 6);
558:
559: listener.reset();
560: model.moveLeadSelectionIndex(-1);
561: assertEquals(6, model.getLeadSelectionIndex());
562: assertEquals(0, listener.getEvents().size());
563:
564: listener.reset();
565: model.moveLeadSelectionIndex(2);
566: assertEquals(2, model.getLeadSelectionIndex());
567: checkIntervalState(0, 2, false);
568: checkIntervalState(3, 6, true);
569: checkIntervalState(7, 10, false);
570: checkSingleEvent(2, 6, false);
571:
572: listener.reset();
573: model.moveLeadSelectionIndex(8);
574: assertEquals(8, model.getLeadSelectionIndex());
575: checkIntervalState(0, 2, false);
576: checkIntervalState(3, 6, true);
577: checkIntervalState(7, 10, false);
578: checkSingleEvent(2, 8, false);
579: }
580:
581: public void testToString() throws Exception {
582: assertNotNull(model.toString());
583: }
584:
585: private void checkIntervalState(final int beginIndex,
586: final int endIndex, final boolean selected) {
587: for (int i = beginIndex; i <= endIndex; i++) {
588: assertEquals(selected, model.isSelectedIndex(i));
589: }
590: }
591:
592: private void checkSingleEvent(final int beginIndex,
593: final int endIndex, final boolean isAdjusting) {
594: checkSingleEvent(listener, beginIndex, endIndex, isAdjusting);
595: }
596:
597: private void checkSingleEvent(final TestListener listener,
598: final int beginIndex, final int endIndex,
599: final boolean isAdjusting) {
600: assertEquals(1, listener.getEvents().size());
601: ListSelectionEvent event = listener.getEvents().get(0);
602: assertEquals(model, event.getSource());
603: assertEquals(beginIndex, event.getFirstIndex());
604: assertEquals(endIndex, event.getLastIndex());
605: assertEquals(isAdjusting, event.getValueIsAdjusting());
606: }
607:
608: private class TestListener implements ListSelectionListener {
609: private List<ListSelectionEvent> events = new ArrayList<ListSelectionEvent>();
610:
611: public void valueChanged(final ListSelectionEvent event) {
612: events.add(event);
613: }
614:
615: public List<ListSelectionEvent> getEvents() {
616: return events;
617: }
618:
619: public void reset() {
620: events.clear();
621: }
622: }
623:
624: public void testIsValidInterval() throws Exception {
625: // regression test for HARMONY-1965
626: try {
627: model.addSelectionInterval(-2, 0);
628: fail("addSelectionInterval should throw IndexOutOfBoundsException");
629: } catch (IndexOutOfBoundsException e) {
630: }
631: try {
632: model.addSelectionInterval(0, -2);
633: fail("addSelectionInterval should throw IndexOutOfBoundsException");
634: } catch (IndexOutOfBoundsException e) {
635: }
636: try {
637: model.setSelectionInterval(-2, 0);
638: fail("setSelectionInterval should throw IndexOutOfBoundsException");
639: } catch (IndexOutOfBoundsException e) {
640: }
641: try {
642: model.setSelectionInterval(0, -2);
643: fail("setSelectionInterval should throw IndexOutOfBoundsException");
644: } catch (IndexOutOfBoundsException e) {
645: }
646: try {
647: model.removeSelectionInterval(-2, 0);
648: fail("removeSelectionInterval should throw IndexOutOfBoundsException");
649: } catch (IndexOutOfBoundsException e) {
650: }
651: try {
652: model.removeSelectionInterval(0, -2);
653: fail("removeSelectionInterval should throw IndexOutOfBoundsException");
654: } catch (IndexOutOfBoundsException e) {
655: }
656: try {
657: model.insertIndexInterval(-2, 0, true);
658: fail("insertIndexInterval should throw IndexOutOfBoundsException");
659: } catch (IndexOutOfBoundsException e) {
660: }
661: if (isHarmony()) {
662: try {
663: model.insertIndexInterval(0, -2, true);
664: fail("insertIndexInterval should throw IndexOutOfBoundsException");
665: } catch (IndexOutOfBoundsException e) {
666: }
667: }
668: try {
669: model.removeIndexInterval(-2, 0);
670: fail("removeIndexInterval should throw IndexOutOfBoundsException");
671: } catch (IndexOutOfBoundsException e) {
672: }
673: try {
674: model.removeIndexInterval(0, -2);
675: fail("removeIndexInterval should throw IndexOutOfBoundsException");
676: } catch (IndexOutOfBoundsException e) {
677: }
678: }
679: }
|