1 /*******************************************************************************
2 * Copyright (c) 2000, 2018 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.tests.junit;
15
16 import static org.junit.Assert.assertArrayEquals;
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertTrue;
20 import static org.junit.Assert.fail;
21
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.SWTError;
24 import org.eclipse.swt.SWTException;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.events.SelectionListener;
27 import org.eclipse.swt.graphics.Font;
28 import org.eclipse.swt.graphics.FontData;
29 import org.eclipse.swt.widgets.List;
30 import org.junit.Before;
31 import org.junit.Test;
32
33 /**
34 * Automated Test Suite for class org.eclipse.swt.widgets.List
35 *
36 * @see org.eclipse.swt.widgets.List
37 */
38 public class Test_org_eclipse_swt_widgets_List extends Test_org_eclipse_swt_widgets_Scrollable {
39
40 @Override
41 @Before
setUp()42 public void setUp() {
43 super.setUp();
44 list = new List(shell, SWT.MULTI);
45
46 setWidget(list);
47 }
48
49 @Override
50 @Test
test_ConstructorLorg_eclipse_swt_widgets_CompositeI()51 public void test_ConstructorLorg_eclipse_swt_widgets_CompositeI() {
52 try {
53 list = new List(null, 0);
54 fail("No exception thrown"); //should never get here
55 } catch (IllegalArgumentException e) {
56 }
57
58 int[] cases =
59 {
60 0,
61 SWT.SINGLE,
62 SWT.MULTI,
63 SWT.MULTI | SWT.V_SCROLL,
64 SWT.MULTI | SWT.H_SCROLL,
65 SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL };
66 for (int style : cases)
67 list = new List(shell, style);
68 }
69
70 @Test
test_addLjava_lang_String()71 public void test_addLjava_lang_String() {
72 try {
73 list.add(null);
74 fail("No exception thrown");
75 } catch (IllegalArgumentException e) {
76 }
77 list.add("");
78 assertArrayEquals(":a:", new String[] {""}, list.getItems());
79 list.add("some \n text");
80 assertArrayEquals(":b:", new String[] {"", "some \n text"}, list.getItems());
81 list.add("some text");
82 assertArrayEquals(":c:", new String[] {"", "some \n text", "some text"}, list.getItems());
83
84 // test single-selection list
85
86 setSingleList();
87
88 try {
89 list.add(null);
90 fail("No exception thrown");
91 } catch (IllegalArgumentException e) {
92 }
93
94 list.add("");
95 assertArrayEquals(":a:", new String[] {""}, list.getItems());
96 list.add("some \n text");
97 assertArrayEquals(":b:", new String[] {"", "some \n text"}, list.getItems());
98 list.add("some text");
99 assertArrayEquals(":c:", new String[] {"", "some \n text", "some text"}, list.getItems());
100 }
101
102 @Test
test_addLjava_lang_StringI()103 public void test_addLjava_lang_StringI() {
104 try {
105 list.add("some text", 2);
106 fail("No exception thrown");
107 } catch (IllegalArgumentException e) {
108 }
109 assertEquals(0, list.getItemCount());
110
111 list.add("", 0);
112 assertArrayEquals(":a:", new String[] {""}, list.getItems());
113 list.add("some \n text", 1);
114 assertArrayEquals(":b:", new String[] {"", "some \n text"}, list.getItems());
115 list.add("some text", 0);
116 assertArrayEquals(":c:", new String[] {"some text", "", "some \n text" }, list.getItems());
117
118 try {
119 list.add(null, 0);
120 fail("No exception thrown string == null");
121 } catch (IllegalArgumentException e) {
122 }
123
124 try {
125 list.add("string", -1);
126 fail("No exception thrown index < 0");
127 } catch (IllegalArgumentException e) {
128 }
129
130 // test single-selection list
131
132 setSingleList();
133
134 try {
135 list.add("some text", 2);
136 fail("No exception thrown");
137 } catch (IllegalArgumentException e) {
138 }
139
140 assertEquals(0, list.getItemCount());
141
142 list.add("", 0);
143 assertArrayEquals(":a:", new String[] {""}, list.getItems());
144 list.add("some \n text", 1);
145 assertArrayEquals(":b:", new String[] {"", "some \n text"}, list.getItems());
146 list.add("some text", 0);
147 assertArrayEquals(":c:", new String[] {"some text", "", "some \n text" }, list.getItems());
148
149 try {
150 list.add(null, 0);
151 fail("No exception thrown string == null");
152 } catch (IllegalArgumentException e) {
153 }
154
155 try {
156 list.add("string", -1);
157 fail("No exception thrown index < 0");
158 } catch (IllegalArgumentException e) {
159 }
160 }
161
162 @Test
test_addSelectionListenerLorg_eclipse_swt_events_SelectionListener()163 public void test_addSelectionListenerLorg_eclipse_swt_events_SelectionListener() {
164 listenerCalled = false;
165 boolean exceptionThrown = false;
166 SelectionListener listener = new SelectionListener() {
167 @Override
168 public void widgetSelected(SelectionEvent event) {
169 listenerCalled = true;
170 }
171 @Override
172 public void widgetDefaultSelected(SelectionEvent event) {
173 }
174 };
175 try {
176 list.addSelectionListener(null);
177 }
178 catch (IllegalArgumentException e) {
179 exceptionThrown = true;
180 }
181 assertTrue("Expected exception not thrown for listener == null", exceptionThrown);
182
183 list.addSelectionListener(listener);
184 list.select(0);
185 assertFalse(":a:", listenerCalled);
186 list.removeSelectionListener(listener);
187 exceptionThrown = false;
188 try {
189 list.removeSelectionListener(null);
190 }
191 catch (IllegalArgumentException e) {
192 exceptionThrown = true;
193 }
194 assertTrue("Expected exception not thrown for listener == null", exceptionThrown);
195
196 // test single-selection list
197
198 setSingleList();
199
200 listenerCalled = false;
201 exceptionThrown = false;
202 try {
203 list.addSelectionListener(null);
204 }
205 catch (IllegalArgumentException e) {
206 exceptionThrown = true;
207 }
208 assertTrue("Expected exception not thrown for listener == null", exceptionThrown);
209
210 list.addSelectionListener(listener);
211 list.select(0);
212 assertFalse(":a:", listenerCalled);
213 list.removeSelectionListener(listener);
214 exceptionThrown = false;
215 try {
216 list.removeSelectionListener(null);
217 }
218 catch (IllegalArgumentException e) {
219 exceptionThrown = true;
220 }
221 assertTrue("Expected exception not thrown for listener == null", exceptionThrown);
222 }
223
224 @Test
test_addSelectionListenerWidgetSelectedAdapterLorg_eclipse_swt_events_SelectionListener()225 public void test_addSelectionListenerWidgetSelectedAdapterLorg_eclipse_swt_events_SelectionListener() {
226 listenerCalled = false;
227 SelectionListener listener = SelectionListener.widgetSelectedAdapter(e -> listenerCalled = true);
228
229 list.addSelectionListener(listener);
230 list.select(0);
231 assertFalse(":a:", listenerCalled);
232 list.removeSelectionListener(listener);
233
234 // test single-selection list
235
236 setSingleList();
237
238 list.addSelectionListener(listener);
239 list.select(0);
240 assertFalse(":a:", listenerCalled);
241 list.removeSelectionListener(listener);
242 }
243
244 @Override
245 @Test
test_computeSizeIIZ()246 public void test_computeSizeIIZ() {
247 // super class test is sufficient
248 }
249
250 @Test
test_deselect$I()251 public void test_deselect$I() {
252 String[] items = { "item0", "item1", "item2", "item3" };
253 list.setItems(items);
254 list.setSelection(items);
255 assertArrayEquals(":a:", list.getSelection(), items);
256 try {
257 list.deselect(null);
258 fail("No exception thrown");
259 } catch (IllegalArgumentException e) {
260 }
261 assertArrayEquals(list.getSelection(), items);
262 list.deselect(new int[] {
263 });
264 assertArrayEquals(list.getSelection(), items);
265 list.deselect(new int[] { 0 });
266 assertEquals(list.isSelected(0), false);
267 assertTrue(list.isSelected(1));
268 list.deselect(new int[] { 2, 0, 0 });
269 assertArrayEquals(list.getSelectionIndices(), new int[] { 1, 3 });
270 /* assert(":d:", !list.isSelected(0));
271 assert(":dd:", !list.isSelected(2));
272 assert(":ddd:", list.isSelected(1));
273 assert(":ddd:", list.isSelected(1));*/
274
275
276 setSingleList();
277 list.setItems(items);
278 list.setSelection(new String[] { "item3" });
279 assertArrayEquals(list.getSelection(), new String[] { "item3" });
280 try {
281 list.deselect(null);
282 fail("No exception thrown");
283 } catch (IllegalArgumentException e) {
284 }
285
286 assertArrayEquals(list.getSelection(), new String[] { "item3" });
287 list.deselect(new int[] {});
288 assertArrayEquals(list.getSelection(), new String[] { "item3" });
289 list.deselect(new int[] { 1 });
290 assertArrayEquals(list.getSelectionIndices(), new int[] { 3 });
291 list.deselect(new int[] { 0 });
292 assertArrayEquals(list.getSelectionIndices(), new int[] { 3 });
293 list.deselect(new int[] { 3 });
294 assertArrayEquals(list.getSelectionIndices(), new int[] {});
295 list.deselect(new int[] { 2, 0, 0 });
296 assertArrayEquals(list.getSelectionIndices(), new int[] {});
297
298 }
299
300 @Test
test_deselectAll()301 public void test_deselectAll() {
302 String[] items = { "item0", "item1", "item2", "item3" };
303 String[] empty = {
304 };
305 list.setItems(items);
306 list.setSelection(items);
307 assertArrayEquals(items, list.getSelection());
308 list.deselectAll();
309 assertArrayEquals(empty, list.getSelection());
310
311
312 setSingleList();
313
314 list.setItems(items);
315 list.setSelection(new String[] { "item3" });
316 assertArrayEquals(new String[] { "item3" }, list.getSelection());
317 list.deselectAll();
318 assertArrayEquals(empty, list.getSelection());
319
320 }
321
322 @Test
test_deselectI()323 public void test_deselectI() {
324 int number = 5;
325 String[] items = new String[number];
326 for (int i = 0; i < number; i++)
327 items[i] = "fred" + i;
328 list.setItems(items);
329
330 String[] items2 = { "item0", "item1", "item2", "item3" };
331 list.setItems(items2);
332 list.setSelection(items2);
333 assertArrayEquals(items2, list.getSelection());
334 list.deselect(5);
335 assertArrayEquals(items2, list.getSelection());
336 list.deselect(1);
337 assertFalse(list.isSelected(1));
338 list.deselect(1);
339 assertFalse(list.isSelected(1));
340
341
342 setSingleList();
343 list.setItems(items2);
344 list.setSelection(new String[] { "item3" });
345 assertArrayEquals(new String[] { "item3" }, list.getSelection());
346 list.deselect(5);
347 assertArrayEquals(new String[] { "item3" }, list.getSelection());
348 list.deselect(2);
349 assertFalse(list.isSelected(2));
350 list.deselect(1);
351 assertFalse(list.isSelected(1));
352 list.deselect(1);
353 assertFalse(list.isSelected(1));
354 }
355
356 @Test
test_deselectII()357 public void test_deselectII() {
358 int number = 5;
359 String[] items = new String[number];
360 for (int i = 0; i < number; i++)
361 items[i] = "fred" + i;
362 list.setItems(items);
363 list.setSelection(items);
364
365 // tests if deselect(i, j) is the same as for (i=0; i<=j; ++i) deselect(i);
366 int[][] cases = { { 3, 1 }, {
367 -3, -2 }, {
368 -2, -1 }, {
369 -1, -1 }, {
370 10, 1 }
371 };
372
373 for (int[] position : cases) {
374 deselectII_helper(items, position[0], position[1], new int[] { 0, 1, 2, 3, 4 });
375 }
376 if (SwtTestUtil.fCheckSWTPolicy) {
377 deselectII_helper(items, -1, 3, new int[] { 4 });
378 deselectII_helper(items, -1, 30, new int[] {
379 });
380 }
381 deselectII_helper(items, 1, 3, new int[] { 0, 4 });
382 deselectII_helper(items, 1, 1, new int[] { 0, 2, 3, 4 });
383 // done
384
385 String[] items2 = { "item0", "item1", "item2", "item3" };
386 list.setItems(items2);
387 list.setSelection(items2);
388 assertArrayEquals(":a:", items2, list.getSelection());
389 list.deselect(0, 0);
390 assertArrayEquals(":b:", list.getSelectionIndices(), new int[] { 1, 2, 3 });
391 list.deselect(0, 0);
392 assertArrayEquals(":bbb:", list.getSelectionIndices(), new int[] { 1, 2, 3 });
393 list.deselect(2, 3);
394 assertArrayEquals(":bb:", list.getSelectionIndices(), new int[] { 1 });
395
396 list.setSelection(items2);
397 list.deselect(0, 2);
398 assertArrayEquals(":dddd:", list.getSelectionIndices(), new int[] { 3 });
399
400 list.setSelection(items2);
401 list.deselect(2, 0);
402 assertArrayEquals(
403 ":ddddd:",
404 list.getSelectionIndices(), new int[] { 0, 1, 2, 3 });
405
406
407 setSingleList();
408
409 list.setItems(items2);
410 list.deselectAll();
411
412 list.select(0);
413
414 list.deselect(-3, -2);
415 assertArrayEquals(list.getSelectionIndices(), new int[] { 0 });
416
417 list.deselect(-2, -1);
418 assertArrayEquals(list.getSelectionIndices(), new int[] { 0 });
419
420 list.deselect(-1, -1);
421 assertArrayEquals(":e:", list.getSelectionIndices(), new int[] { 0 });
422
423
424 list.setSelection(new String[] { "item3" });
425 assertArrayEquals(list.getSelection(), new String[] { "item3" });
426
427 list.deselect(1, 1);
428 assertArrayEquals(list.getSelection(), new String[] { "item3" });
429
430 list.deselect(0, 0);
431 assertArrayEquals(list.getSelection(), new String[] { "item3" });
432
433 list.deselect(3, 3);
434 assertArrayEquals(list.getSelection(), new String[] {});
435
436 list.setSelection(new String[] { "item3" });
437 list.deselect(1, 2);
438 assertArrayEquals(list.getSelection(), new String[] { "item3" });
439
440 list.setSelection(new String[] { "item3" });
441 list.deselect(0, 2);
442 assertArrayEquals(list.getSelectionIndices(), new int[] { 3 });
443
444 list.setSelection(new String[] { "item3" });
445 list.deselect(1, 3);
446 assertArrayEquals(list.getSelectionIndices(), new int[] {});
447 }
448
449 @Test
test_getFocusIndex()450 public void test_getFocusIndex() {
451 String[] items = { "item0", "item1", "item2"};
452 list.setItems(items);
453 list.setSelection(0);
454 assertEquals(0, list.getFocusIndex());
455 list.setSelection(2);
456 assertEquals(2, list.getFocusIndex());
457 }
458
459 @Test
test_getItemCount()460 public void test_getItemCount() {
461 String[] items = { "item0", "item1", "item2", "item3" };
462
463 assertEquals(0, list.getItemCount());
464 list.setItems(items);
465 assertEquals(4, list.getItemCount());
466 list.remove(2);
467 assertEquals(3, list.getItemCount());
468 list.removeAll();
469 assertEquals(0, list.getItemCount());
470
471
472 setSingleList();
473 assertEquals(0, list.getItemCount());
474 list.setItems(items);
475 assertEquals(4, list.getItemCount());
476 list.remove(2);
477 assertEquals(3, list.getItemCount());
478 list.removeAll();
479 assertEquals(0, list.getItemCount());
480
481 }
482
483 @Test
test_getItemHeight()484 public void test_getItemHeight() {
485 if (SwtTestUtil.isGTK) {
486 //TODO Fix GTK failure.
487 if (SwtTestUtil.verbose) {
488 System.out.println("Excluded test_getItemHeight(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_widgets_List)");
489 }
490 return;
491 }
492 FontData fontData = list.getFont().getFontData()[0];
493 int lineHeight;
494 Font font;
495
496 font = new Font(list.getDisplay(), fontData.getName(), 8, fontData.getStyle());
497 list.setFont(font);
498 lineHeight = list.getItemHeight();
499 list.setFont(null);
500 font.dispose();
501 font = new Font(list.getDisplay(), fontData.getName(), 12, fontData.getStyle());
502 list.setFont(font);
503 int newLineHeight = list.getItemHeight();
504 assertTrue(":a:", newLineHeight > lineHeight);
505 list.setFont(null);
506 font.dispose();
507 }
508
509 @Test
test_getItemI()510 public void test_getItemI() {
511 String[] items = { "item0", "item1", "item2", "item3" };
512 list.setItems(items);
513 try {
514 list.getItem(5);
515 fail("No exception thrown");
516 } catch (IllegalArgumentException e) {
517 }
518
519 try {
520 list.getItem(-1);
521 fail("No exception thrown for index < 0");
522 } catch (IllegalArgumentException e) {
523 }
524
525 assertEquals(list.getItem(3), "item3");
526
527
528 setSingleList();
529 list.setItems(items);
530 try {
531 list.getItem(5);
532 fail("No exception thrown");
533 } catch (IllegalArgumentException e) {
534 }
535
536 try {
537 list.getItem(-1);
538 fail("No exception thrown for index < 0");
539 } catch (IllegalArgumentException e) {
540 }
541
542 //assert(":a:", list.getItem(5)==null);
543 assertEquals("item3", list.getItem(3));
544
545 }
546
547 @Test
test_getItems()548 public void test_getItems() {
549 String[][] cases = { {
550 }, {
551 "" }, {
552 "", "" }, {
553 "text1", "text2" }
554 };
555 for (int i = 0; i < cases.length; i++) {
556 list.setItems(cases[i]);
557 assertArrayEquals("case: " + i, cases[i], list.getItems());
558 }
559 }
560
561 @Test
test_getSelection()562 public void test_getSelection() {
563 String[][] cases = { {
564 }, {
565 "" }, {
566 "", "" }, {
567 "text1", "text2" }
568 };
569 for (int i = 0; i < cases.length; i++) {
570 // System.out.println("loop:" + i);
571 list.setItems(cases[i]);
572 list.setSelection(cases[i]);
573 // System.out.println("list:" + list.getSelection());
574 // System.out.println("case:" + i + cases[i]);
575 assertArrayEquals("case: " + i, cases[i], list.getSelection());
576 }
577
578 for (int i = 1; i < cases.length; i++) {
579 list.setItems(cases[i]);
580 list.setSelection(0);
581 assertArrayEquals(
582 "case: " + String.valueOf(i),
583 list.getSelection(), new String[] { cases[i][0] });
584 }
585
586 String[] items = { "text1", "text2", "text3" };
587 list.setItems(items);
588 int[] sel = { 0, 2 };
589 list.setSelection(sel);
590 assertEquals(list.getSelection().length, 2);
591 String[] selItems = new String[] {items[0], items[2]};
592 assertArrayEquals(list.getSelection(), selItems);
593
594 list.setSelection(0, 1);
595 assertEquals(list.getSelection().length, 2);
596 selItems = new String[] {items[0], items[1]};
597 assertArrayEquals(list.getSelection(), selItems);
598
599 list.setSelection(1, 1);
600 assertEquals(list.getSelection().length, 1);
601 assertEquals(list.getSelection()[0], items[1]);
602
603 list.setSelection(1, 0);
604 String[] empty = {
605 };
606 assertArrayEquals(empty, list.getSelection());
607
608 String[] bogus_items = { "bogus_text1", "bogus_text2", "bogus_text3" };
609 list.setSelection(bogus_items);
610 assertArrayEquals(empty, list.getSelection());
611
612 // test single-selection lists
613
614 setSingleList();
615
616 list.setItems(items);
617 sel = new int[] { 0 };
618 list.setSelection(sel);
619 assertEquals(1, list.getSelection().length);
620 assertEquals(items[0], list.getSelection()[0]);
621
622 list.setSelection(1, 1);
623 assertEquals(1, list.getSelection().length);
624 assertEquals(items[1], list.getSelection()[0]);
625 }
626
627 /**
628 * Returns the number of selected items contained in the receiver.
629 *
630 * @return the number of selected items
631 *
632 * @exception SWTException <ul>
633 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
634 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
635 * </ul>
636 * @exception SWTError <ul>
637 * <li>ERROR_CANNOT_GET_COUNT - if the operation fails because of an operating system failure</li>
638 * </ul>
639 */
640 @Test
test_getSelectionCount()641 public void test_getSelectionCount() {
642 String[] items = { "text1", "text2", "text3" };
643
644 list.setItems(items);
645 assertEquals(0, list.getSelectionCount());
646
647 list.setSelection(items);
648 assertEquals(3, list.getSelectionCount());
649
650 list.deselectAll();
651 try {
652 list.setSelection((String[]) null);
653 fail("No exception thrown for selection == null");
654 } catch (IllegalArgumentException e) {
655 }
656 assertEquals(list.getSelectionCount(), 0);
657
658
659 setSingleList();
660
661 list.setItems(items);
662 list.setSelection(new String[] { "text2" });
663 assertEquals(1, list.getSelectionCount());
664 }
665
666 @Test
test_getSelectionIndex()667 public void test_getSelectionIndex() {
668 String[] items = { "text1", "text2", "text3" };
669
670 // not properly spec'd for multi-select lists
671 list.setItems(items);
672 list.setSelection(items);
673 assertEquals(0, list.getSelectionIndex());
674
675
676 setSingleList();
677
678 list.setItems(items);
679 list.setSelection(items);
680 assertEquals(-1, list.getSelectionIndex());
681
682 list.setSelection(new String[] { "text1" });
683 assertEquals(0, list.getSelectionIndex());
684
685 list.setSelection(new String[] { "text2" });
686 assertEquals(1, list.getSelectionIndex());
687
688 list.setSelection(new String[] { "text3" });
689 assertEquals(2, list.getSelectionIndex());
690
691 list.setSelection(items);
692 assertEquals(-1, list.getSelectionIndex());
693 }
694
695 @Test
test_getSelectionIndices()696 public void test_getSelectionIndices() {
697 String[] items = { "text1", "text2", "text3" };
698
699 list.setItems(items);
700
701 assertArrayEquals(list.getSelectionIndices(), new int[] {});
702
703 list.setSelection(items);
704 int[] sel = { 0, 1, 2 };
705 assertArrayEquals(sel, list.getSelectionIndices());
706
707 list.setSelection(sel);
708 assertArrayEquals(sel, list.getSelectionIndices());
709
710 sel = new int[] { 0 };
711 list.setSelection(sel);
712 assertArrayEquals(sel, list.getSelectionIndices());
713
714 sel = new int[] { 1 };
715 list.setSelection(sel);
716 assertArrayEquals(sel, list.getSelectionIndices());
717
718 sel = new int[] { 2 };
719 list.setSelection(sel);
720 assertArrayEquals(sel, list.getSelectionIndices());
721
722 sel = new int[] { 3 };
723 list.setSelection(sel);
724 assertArrayEquals(new int[] {}, list.getSelectionIndices());
725
726 sel = new int[] { -1, 0, 1, 2, 3 };
727 list.setSelection(sel);
728 assertArrayEquals(new int[] { 0, 1, 2 }, list.getSelectionIndices());
729
730 sel = new int[] { 1, 1, 2, 2 };
731 list.setSelection(sel);
732 assertArrayEquals(new int[] { 1, 2 }, list.getSelectionIndices());
733
734
735 setSingleList();
736
737 list.setItems(items);
738
739 sel = new int[] { 0 };
740 list.setSelection(sel);
741 assertArrayEquals(sel, list.getSelectionIndices());
742
743 sel = new int[] { 1 };
744 list.setSelection(sel);
745 assertArrayEquals(sel, list.getSelectionIndices());
746
747 sel = new int[] { 2 };
748 list.setSelection(sel);
749 assertArrayEquals(sel, list.getSelectionIndices());
750
751 sel = new int[] { 3 };
752 list.setSelection(sel);
753 assertArrayEquals(new int[] {}, list.getSelectionIndices());
754 }
755
756 @Test
test_getTopIndex()757 public void test_getTopIndex() {
758 String[] items = new String [10];
759 for (int i = 0; i < 10; i++) {
760 items[i] = "text" + String.valueOf(i);
761 }
762
763 list.setItems(items);
764 assertEquals(0, list.getTopIndex());
765
766 list.setTopIndex(5);
767 assertEquals(5, list.getTopIndex());
768
769 setSingleList();
770
771 list.setItems(items);
772 assertEquals(0, list.getTopIndex());
773
774 }
775
776 @Test
test_indexOfLjava_lang_String()777 public void test_indexOfLjava_lang_String() {
778 String[] items = { "text1", "text2", "text3" };
779
780 list.setItems(items);
781 assertEquals(list.indexOf("text3"), 2);
782 assertEquals(list.indexOf("text4"), -1);
783
784 try {
785 list.indexOf(null);
786 fail("No exception thrown for item == null");
787 } catch (IllegalArgumentException e) {
788 }
789
790 String[] items2 = { "text1", "text2", "text2" }; //two identical
791
792 list.setItems(items2);
793 assertEquals(list.indexOf("text2"), 1);
794
795
796 setSingleList();
797
798 list.setItems(items);
799 assertEquals(-1, list.indexOf("text3", 4));
800
801
802 assertEquals(2, list.indexOf("text3"));
803
804
805 assertEquals(-1, list.indexOf("text4"));
806
807
808 try {
809 list.indexOf(null);
810 fail("No exception thrown");
811 } catch (IllegalArgumentException e) {
812 }
813
814
815 assertEquals(1, list.indexOf("text2"));
816 }
817
818 @Test
test_indexOfLjava_lang_StringI()819 public void test_indexOfLjava_lang_StringI() {
820 String[] items = { "text1", "text2", "text3" };
821
822 list.setItems(items);
823 assertEquals(-1, list.indexOf("text3", 4));
824 assertEquals(2, list.indexOf("text3", 2));
825 assertEquals(1, list.indexOf("text2", 0));
826 assertEquals(1, list.indexOf("text2", 1));
827 assertEquals(-1, list.indexOf("text2", 2));
828
829 String[] items2 = { "text1", "text2", "text2" }; //two identical
830 list.setItems(items2);
831 assertEquals(list.indexOf("text2", 2), 2);
832
833 try {
834 list.indexOf(null, 0);
835 fail("No exception thrown for string == null");
836 } catch (IllegalArgumentException e) {
837 }
838
839 setSingleList();
840
841 list.setItems(items2);
842 // assert("list.indexOf(\"text2\", -1)==1", list.indexOf("text2", -1)==1);
843 assertEquals(1, list.indexOf("text2", 0));
844 assertEquals(1, list.indexOf("text2", 1));
845 assertEquals(2, list.indexOf("text2", 2));
846
847 try {
848 list.indexOf(null, 0);
849 fail("No exception thrown for string == null");
850 } catch (IllegalArgumentException e) {
851 }
852 }
853
854 @Test
test_isSelectedI()855 public void test_isSelectedI() {
856 String[] items = { "text1", "text2", "text2" }; //two identical
857
858 list.setItems(items);
859 list.setSelection(items);
860 assertTrue(list.isSelected(0));
861 assertTrue(list.isSelected(1));
862 assertTrue(list.isSelected(2));
863 assertFalse(list.isSelected(3));
864
865
866 setSingleList();
867
868 list.setItems(items);
869 list.setSelection(items);
870 if (SwtTestUtil.fCheckSWTPolicy) {
871 assertFalse(list.isSelected(0));
872 assertTrue(list.isSelected(1));
873 assertFalse(list.isSelected(2));
874 assertFalse(list.isSelected(3));
875 }
876
877 }
878
879 @Test
test_remove$I()880 public void test_remove$I() {
881 try {
882 list.remove((int[]) null);
883 fail("No exception thrown for indices == null");
884 } catch (IllegalArgumentException e) {
885 }
886
887 String[] items = { "text0", "text1", "text2", "text3" };
888
889 list.setItems(items);
890 assertEquals(list.getItemCount(), 4);
891
892 list.setItems(items);
893 list.remove(new int[] { 1, 0, 1 });
894 assertEquals(list.getItemCount(), 2);
895
896 list.setItems(items);
897
898 // index > number of elements in list
899 try {
900 list.remove(new int[] { 4, 1});
901 fail("No exception thrown");
902 } catch (IllegalArgumentException e) {
903 }
904 assertArrayEquals(":a:", list.getItems(), items);
905
906 try {
907 list.remove(new int[] { 3, 1, -1 });
908 fail("No exception thrown");
909 } catch (IllegalArgumentException e) {
910 }
911 assertArrayEquals(":a:", list.getItems(), items);
912
913 list.setItems(items);
914 assertEquals(list.getItemCount(), 4);
915
916 try {
917 list.remove(new int[] { -1, -1 });
918 fail("No exception thrown");
919 } catch (IllegalArgumentException e) {
920 }
921 assertArrayEquals(":b:", list.getItems(), items);
922
923 try {
924 list.remove(new int[] { -2, -1 });
925 fail("No exception thrown");
926 } catch (IllegalArgumentException e) {
927 }
928 assertArrayEquals(":c:", list.getItems(), items);
929
930 list.setItems(items);
931 assertEquals(list.getItemCount(), 4);
932
933 list.remove(new int[] { 1, 1, 1 });
934 assertArrayEquals(
935 ":d:",
936 list.getItems(), new String[] { "text0", "text2", "text3" });
937
938 list.setItems(items);
939 assertEquals(list.getItemCount(), 4);
940
941 list.remove(new int[] { 1, 3 });
942 assertArrayEquals(":e:", list.getItems(), new String[] { "text0", "text2" });
943
944
945 setSingleList();
946
947 try {
948 int[] indices = null;
949 list.remove(indices);
950 fail("No exception thrown for indices == null");
951 } catch (IllegalArgumentException e) {
952 }
953
954 list.setItems(items);
955 assertEquals(4, list.getItemCount());
956
957 list.remove(new int[] { 1, 3 });
958 assertArrayEquals(":f:", list.getItems(), new String[] { "text0", "text2" });
959
960
961 list.setItems(items);
962 assertEquals(4, list.getItemCount());
963
964 list.remove(new int[] { 3, 1 });
965 assertArrayEquals(":g:", list.getItems(), new String[] { "text0", "text2" });
966
967
968 list.setItems(items);
969 assertEquals(4, list.getItemCount());
970
971 // index > number of elements in list
972 try {
973 list.remove(new int[] { 4, 1});
974 fail("No exception thrown");
975 } catch (IllegalArgumentException e) {
976 }
977 assertArrayEquals(":h:", list.getItems(), items);
978
979 try {
980 list.remove(new int[] { 3, 1, -1 });
981 fail("No exception thrown");
982 } catch (IllegalArgumentException e) {
983 }
984 assertArrayEquals(":h:", list.getItems(), items);
985
986
987 list.setItems(items);
988 assertEquals(4, list.getItemCount());
989
990 try {
991 list.remove(new int[] { -1, -1 });
992 fail("No exception thrown");
993 } catch (IllegalArgumentException e) {
994 }
995
996 assertArrayEquals(":i:", items, list.getItems());
997
998
999 assertEquals(4, list.getItemCount());
1000
1001 list.remove(new int[] { 1, 1, 1 });
1002 assertArrayEquals(":j:",
1003 new String[] { "text0", "text2", "text3" }, list.getItems());
1004
1005 }
1006
1007 @Test
test_removeAll()1008 public void test_removeAll() {
1009 String[] items = { "text1", "text2", "text3", "test2" };
1010
1011 list.setItems(items);
1012 assertEquals(list.getItemCount(), 4);
1013
1014 list.removeAll();
1015 assertEquals(list.getItemCount(), 0);
1016 list.removeAll();
1017 assertEquals(list.getItemCount(), 0);
1018
1019
1020 setSingleList();
1021 list.setItems(items);
1022 assertEquals(4, list.getItemCount());
1023
1024 list.removeAll();
1025 assertEquals(0, list.getItemCount());
1026
1027
1028 setSingleList();
1029 assertEquals(0, list.getItemCount());
1030 list.removeAll();
1031 assertEquals(0, list.getItemCount());
1032
1033 }
1034
1035 @Test
test_removeI()1036 public void test_removeI() {
1037 String[] items = { "text1", "text2", "text3" };
1038
1039 list.setItems(items);
1040 assertEquals(list.getItemCount(), 3);
1041
1042 try {
1043 list.remove(3);
1044 fail("No exception thrown");
1045 } catch (IllegalArgumentException e) {
1046 }
1047 assertEquals(list.getItemCount(), 3);
1048
1049 try {
1050 list.remove(-1);
1051 fail("No exception thrown");
1052 } catch (IllegalArgumentException e) {
1053 }
1054 assertEquals(list.getItemCount(), 3);
1055
1056 list.remove(1);
1057 assertEquals(list.getItemCount(), 2);
1058 assertEquals(list.getItem(1), "text3");
1059
1060 list.setItems(items);
1061 assertEquals(list.getItemCount(), 3);
1062
1063 try {
1064 list.remove(3, 4);
1065 fail("No exception thrown");
1066 } catch (IllegalArgumentException e) {
1067 }
1068
1069 assertEquals(list.getItemCount(), 3);
1070
1071 try {
1072 list.remove(3, 3);
1073 fail("No exception thrown");
1074 } catch (IllegalArgumentException e) {
1075 }
1076
1077 assertEquals(list.getItemCount(), 3);
1078
1079 list.remove(0);
1080 assertEquals(list.getItemCount(), 2);
1081 list.remove(0);
1082 assertEquals(list.getItemCount(), 1);
1083 assertEquals(list.getItem(0), "text3");
1084 list.remove(0);
1085 assertEquals(list.getItemCount(), 0);
1086
1087 list.setItems(items);
1088 list.remove(1, 2);
1089 assertEquals(list.getItemCount(), 1);
1090 assertEquals(list.getItem(0), "text1");
1091
1092
1093 setSingleList();
1094 list.setItems(items);
1095 assertEquals(3, list.getItemCount());
1096 try {
1097 list.remove(3);
1098 fail("No exception thrown");
1099 } catch (IllegalArgumentException e) {
1100 }
1101 assertEquals(3, list.getItemCount());
1102 /////////////////////////////////////////////////
1103 try {
1104 list.remove(-1);
1105 fail("No exception thrown");
1106 } catch (IllegalArgumentException e) {
1107 }
1108
1109 assertEquals(3, list.getItemCount());
1110 ////////////////////////////////////////////////
1111 list.remove(1);
1112 assertEquals(2, list.getItemCount());
1113 //////////////////////////////////////////////////////
1114 assertTrue(list.getItem(1).equals("text3"));
1115
1116 list.setItems(items);
1117 assertEquals(list.getItemCount(), 3);
1118
1119 list.remove(0);
1120 assertEquals(list.getItemCount(), 2);
1121 list.remove(0);
1122 assertEquals(list.getItemCount(), 1);
1123 assertEquals(list.getItem(0), "text3");
1124 list.remove(0);
1125 assertEquals(list.getItemCount(), 0);
1126
1127 }
1128
1129 @Test
test_removeII()1130 public void test_removeII() {
1131 String[] items = { "text1", "text2", "text3" };
1132
1133 list.setItems(items);
1134 assertEquals(3, list.getItemCount());
1135
1136 try {
1137 list.remove(3, 4);
1138 fail("No exception thrown");
1139 } catch (IllegalArgumentException e) {
1140 }
1141
1142 assertEquals(3, list.getItemCount());
1143
1144 try {
1145 list.remove(3, 3);
1146 fail("No exception thrown");
1147 } catch (IllegalArgumentException e) {
1148 }
1149 assertEquals(3, list.getItemCount());
1150
1151 list.remove(0, 0);
1152 assertEquals(2, list.getItemCount());
1153 assertEquals("text3", list.getItem(1));
1154
1155 list.setItems(items);
1156 assertEquals(3, list.getItemCount());
1157
1158 try {
1159 list.remove(-1, 1);
1160 fail("No exception thrown for start index < 0");
1161 } catch (IllegalArgumentException e) {
1162 }
1163 assertEquals(3, list.getItemCount());
1164
1165 try {
1166 list.remove(3, 4);
1167 fail("No exception thrown");
1168 } catch (IllegalArgumentException e) {
1169 }
1170 assertEquals(3, list.getItemCount());
1171
1172 list.remove(0, 2);
1173 assertEquals(0, list.getItemCount());
1174
1175 list.setItems(items);
1176 assertEquals(3, list.getItemCount());
1177
1178 try {
1179 list.remove(3, 3);
1180 fail("No exception thrown");
1181 } catch (IllegalArgumentException e) {
1182 }
1183 assertEquals(3, list.getItemCount());
1184
1185 list.remove(2, 0);
1186 assertEquals(3, list.getItemCount());
1187
1188
1189 setSingleList();
1190
1191 list.setItems(items);
1192 assertEquals(3, list.getItemCount());
1193 //////////////////////////////////////////////////////////////
1194 try {
1195 list.remove(3, 4);
1196 fail("No exception thrown");
1197 } catch (IllegalArgumentException e) {
1198 }
1199
1200 assertEquals(3, list.getItemCount());
1201 /////////////////////////////////////////////////////////
1202 try {
1203 list.remove(3, 3);
1204 fail("No exception thrown");
1205 } catch (IllegalArgumentException e) {
1206 }
1207
1208 assertEquals(3, list.getItemCount());
1209 //////////////////////////////////////////////////////////////
1210
1211 try {
1212 list.remove(-1, 1);
1213 fail("No exception thrown for start index < 0");
1214 } catch (IllegalArgumentException e) {
1215 }
1216 assertEquals(3, list.getItemCount());
1217
1218 list.remove(1, 2);
1219 assertEquals(1, list.getItemCount());
1220 assertEquals("text1", list.getItem(0));
1221
1222
1223 list.setItems(items);
1224 assertEquals(3, list.getItemCount());
1225
1226 try {
1227 list.remove(2, 10);
1228 fail("No exception thrown");
1229 } catch (IllegalArgumentException e) {
1230 }
1231 assertEquals(3, list.getItemCount());
1232 assertEquals("text2", list.getItem(1));
1233
1234 list.remove(2, 0);
1235 assertEquals(3, list.getItemCount());
1236 }
1237
1238 @Test
test_removeLjava_lang_String()1239 public void test_removeLjava_lang_String() {
1240 String[] items = { "text1", "text2", "text3", "test2" };
1241
1242 list.setItems(items);
1243 assertEquals(list.getItemCount(), 4);
1244
1245 try {
1246 list.remove((String) null);
1247 fail("No exception thrown");
1248 } catch (IllegalArgumentException e) {
1249 }
1250 assertEquals(list.getItemCount(), 4);
1251
1252 try {
1253 list.remove("items989");
1254 fail("No exception thrown");
1255 } catch (IllegalArgumentException e) {
1256 }
1257 assertEquals(list.getItemCount(), 4);
1258
1259 list.setItems(items);
1260 assertEquals(list.getItemCount(), 4);
1261
1262 list.remove("text3");
1263 assertEquals(list.getItemCount(), 3);
1264
1265 list.remove("text2");
1266 assertEquals(list.getItemCount(), 2);
1267
1268
1269 setSingleList();
1270 list.setItems(items);
1271 assertEquals(4, list.getItemCount());
1272
1273 try {
1274 list.remove((String) null);
1275 fail("No exception thrown");
1276 } catch (IllegalArgumentException e) {
1277 }
1278 assertEquals(4, list.getItemCount());
1279 ////////////////////////////////////////
1280 try {
1281 list.remove("items989");
1282 fail("No exception thrown");
1283 } catch (IllegalArgumentException e) {
1284 }
1285 assertEquals(4, list.getItemCount());
1286
1287
1288 assertEquals(4, list.getItemCount());
1289
1290 list.remove("text3");
1291 assertEquals(3, list.getItemCount());
1292
1293 list.remove("text2");
1294 assertEquals(2, list.getItemCount());
1295
1296 }
1297
1298 @Test
test_select$I()1299 public void test_select$I() {
1300 try {
1301 list.select((int[]) null);
1302 fail("No exception thrown");
1303 } catch (IllegalArgumentException e) {
1304 }
1305
1306 String[] items = { "item0", "item1", "item2", "item3" };
1307 list.setItems(items);
1308
1309 list.select(new int[] { 0, 2 });
1310 assertArrayEquals(list.getSelectionIndices(), new int[] { 0, 2 });
1311
1312 list.select(new int[] { 1, 3 });
1313 assertArrayEquals(list.getSelectionIndices(), new int[] { 0, 1, 2, 3 });
1314
1315 list.select(new int[] { 1, 3 });
1316 assertArrayEquals(list.getSelectionIndices(), new int[] { 0, 1, 2, 3 });
1317
1318 list.select(new int[] { 1 });
1319 assertArrayEquals(list.getSelectionIndices(), new int[] { 0, 1, 2, 3 });
1320
1321 list.add("item4");
1322
1323 list.select(new int[] { -1, 0, 1, 2, 3 });
1324 assertArrayEquals(list.getSelectionIndices(), new int[] { 0, 1, 2, 3 });
1325
1326 list.deselectAll();
1327 list.select(new int[] { 1, 2, 3 });
1328 assertArrayEquals(list.getSelectionIndices(), new int[] { 1, 2, 3 });
1329
1330 int[] ind = new int[32];
1331 for (int i = 0; i < ind.length; i++) ind[i] = i;
1332 list.select(ind);
1333 assertArrayEquals(list.getSelectionIndices(), new int[] { 0, 1, 2, 3, 4 });
1334
1335 list.setSelection(new int[] {});
1336 list.select(new int[] { 1 });
1337 assertArrayEquals(list.getSelectionIndices(), new int[] { 1 });
1338
1339 list.setSelection(new int[] {});
1340 list.select(new int[] { -1 });
1341 assertArrayEquals(list.getSelectionIndices(), new int[] {});
1342
1343 list.deselectAll();
1344 items = list.getItems();
1345
1346 select$I_helper(0, 3, new int[] { 0, 1, 2, 3 });
1347 select$I_helper(-1, 3, new int[] { 0, 1, 2, 3 });
1348 select$I_helper(-1, 30, new int[] { 0, 1, 2, 3, 4 });
1349
1350
1351 /*--- Single-select ---*/
1352
1353 setSingleList();
1354 list.setItems(items);
1355
1356 try {
1357 list.select((int[]) null);
1358 fail("No exception thrown");
1359 } catch (IllegalArgumentException e) {
1360 }
1361
1362 list.select(new int[]{ -1 });
1363 assertArrayEquals(list.getSelectionIndices(), new int[] {});
1364
1365 list.select(new int[] { 0 });
1366 assertArrayEquals(list.getSelectionIndices(), new int[] { 0 });
1367
1368 list.select(new int[] { 1 });
1369 assertArrayEquals(list.getSelectionIndices(), new int[] { 1 });
1370
1371 list.select(new int[] { 2 });
1372 assertArrayEquals(list.getSelectionIndices(), new int[] { 2 });
1373
1374 list.select(new int[] { 3 });
1375 assertArrayEquals(list.getSelectionIndices(), new int[] { 3 });
1376
1377 list.select(new int[] { 4 });
1378 assertArrayEquals(list.getSelectionIndices(), new int[] { 4 });
1379
1380 list.select(new int[] { 5 });
1381 assertArrayEquals(list.getSelectionIndices(), new int[] { 4 });
1382
1383 list.deselectAll();
1384 list.select(new int[]{ 0, 1, 2, 3 });
1385 assertArrayEquals(list.getSelectionIndices(), new int[] {});
1386
1387 list.select(new int[]{ -1, 0, 1, 2, 3 });
1388 assertArrayEquals(list.getSelectionIndices(), new int[] {});
1389
1390 int[] selection = new int[32];
1391 for (int i = 0; i < selection.length; i++) selection[i] = i;
1392 list.select(selection);
1393 assertArrayEquals(list.getSelectionIndices(), new int[] {});
1394
1395 list.select(new int[]{ 1, 1, 1 });
1396 assertArrayEquals(list.getSelectionIndices(), new int[]{});
1397 }
1398
1399 @Test
test_selectAll()1400 public void test_selectAll() {
1401 String[] items = { "text1", "text2", "text3", "test2" };
1402
1403 list.setItems(items);
1404 assertEquals(list.getSelectionCount(), 0);
1405 list.selectAll();
1406 assertEquals(list.getSelectionCount(), 4);
1407
1408
1409 setSingleList();
1410
1411 list.setItems(items);
1412 assertEquals(0, list.getSelectionCount());
1413 list.selectAll();
1414 assertEquals(0, list.getSelectionCount());
1415
1416 }
1417
1418 @Test
test_selectI()1419 public void test_selectI() {
1420 String[] items = { "item0", "item1", "item2", "item3" };
1421 list.setItems(items);
1422
1423 list.select(2);
1424 assertArrayEquals("select(2):", list.getSelectionIndices(), new int[] { 2 });
1425
1426 list.select(1);
1427 assertArrayEquals("select(1):", list.getSelectionIndices(), new int[] { 1, 2 });
1428
1429 list.select(3);
1430 assertArrayEquals(
1431 "select(3):",
1432 list.getSelectionIndices(), new int[] { 1, 2, 3 });
1433
1434 list.select(5);
1435 assertArrayEquals(
1436 "select(5):",
1437 list.getSelectionIndices(), new int[] { 1, 2, 3 });
1438
1439 list.deselectAll();
1440 list.select(0);
1441 assertArrayEquals("select(0):", list.getSelectionIndices(), new int[] { 0 });
1442
1443 list.deselectAll();
1444 list.select(-1);
1445 assertArrayEquals("select(-1):", list.getSelectionIndices(), new int[] {});
1446
1447 list.deselectAll();
1448 list.select(-2);
1449 assertArrayEquals("select(-2):", list.getSelectionIndices(), new int[] {});
1450
1451 list.deselectAll();
1452 list.select(4);
1453 assertArrayEquals("select(4):", list.getSelectionIndices(), new int[] {});
1454
1455 setSingleList();
1456 list.setItems(items);
1457
1458 list.select(2);
1459 assertArrayEquals(list.getSelectionIndices(), new int[] { 2 });
1460
1461 list.select(1);
1462 assertArrayEquals(list.getSelectionIndices(), new int[] { 1 });
1463
1464 list.select(3);
1465 assertArrayEquals(list.getSelectionIndices(), new int[] { 3 });
1466
1467 list.select(5);
1468 assertArrayEquals(list.getSelectionIndices(), new int[] { 3 });
1469
1470 list.deselectAll();
1471 list.select(0);
1472 assertArrayEquals("select(0):", list.getSelectionIndices(), new int[] { 0 });
1473
1474 list.deselectAll();
1475 list.select(-1);
1476 assertArrayEquals("select(-1):", list.getSelectionIndices(), new int[] {});
1477
1478 list.deselectAll();
1479 list.select(-2);
1480 assertArrayEquals("select(-2):", list.getSelectionIndices(), new int[] {});
1481
1482 list.deselectAll();
1483 list.select(4);
1484 assertArrayEquals("select(4):", list.getSelectionIndices(), new int[] {});
1485 }
1486
1487 @Test
test_selectII()1488 public void test_selectII() {
1489 list.select(0, 0);
1490 assertArrayEquals("empty list", list.getSelectionIndices(), new int[] {});
1491
1492 list.select(0, 1);
1493 assertArrayEquals("empty list", list.getSelectionIndices(), new int[] {});
1494
1495 list.select(-1, 0);
1496 assertArrayEquals("empty list", list.getSelectionIndices(), new int[] {});
1497
1498 int number = 5;
1499
1500 String[] items = new String[number];
1501 int[] empty = {};
1502
1503 for (int i = 0; i < number; i++)
1504 items[i] = "item" + i;
1505
1506 selectII_helper(items, 10, 1, empty);
1507 selectII_helper(items, 3, 1, empty);
1508 selectII_helper(items, -1, -1, empty);
1509 selectII_helper(items, 5, 5, empty);
1510 selectII_helper(items, 4, 5, new int[] { 4 });
1511 selectII_helper(items, -1, 0, new int[] { 0 });
1512 selectII_helper(items, 2, 4, new int[] { 2, 3, 4 });
1513 selectII_helper(items, 0, 3, new int[] { 0, 1, 2, 3 });
1514 selectII_helper(items, 1, 1, new int[] { 1 });
1515 selectII_helper(items, -1, 30, new int[] { 0, 1, 2, 3, 4 });
1516 selectII_helper(items, -1, 3, new int[] { 0, 1, 2, 3 });
1517
1518 list.select(0);
1519 assertArrayEquals(list.getSelectionIndices(), new int[] { 0 });
1520
1521 list.select(-10, -9);
1522 assertArrayEquals(list.getSelectionIndices(), new int[] { 0 });
1523 list.deselectAll();
1524
1525 list.select(1000, 2000);
1526 assertEquals(list.getSelectionCount(), 0);
1527
1528 list.deselectAll();
1529 list.select(1, 2);
1530 assertArrayEquals(list.getSelectionIndices(), new int[] { 1, 2 });
1531
1532 list.select(1, 3);
1533 assertArrayEquals(list.getSelectionIndices(), new int[] { 1, 2, 3 });
1534
1535 list.select(2, 2);
1536 assertArrayEquals(list.getSelectionIndices(), new int[] { 1, 2, 3 });
1537
1538 list.select(3, 5);
1539 assertArrayEquals(list.getSelectionIndices(), new int[] { 1, 2, 3, 4 });
1540
1541
1542 setSingleList();
1543 list.select(0, 0);
1544 assertArrayEquals("empty list", list.getSelectionIndices(), new int[] {});
1545
1546 list.select(0, 1);
1547 assertArrayEquals("empty list", list.getSelectionIndices(), new int[] {});
1548
1549 list.select(-1, 0);
1550 assertArrayEquals("empty list", list.getSelectionIndices(), new int[] {});
1551
1552 list.setItems(items);
1553 list.select(0);
1554 assertArrayEquals(list.getSelectionIndices(), new int[] { 0 });
1555 list.select(-10, -9);
1556 assertArrayEquals(list.getSelectionIndices(), new int[] { 0 });
1557
1558 list.deselectAll();
1559 assertEquals(0, list.getSelectionCount());
1560
1561 list.select(1000, 2000);
1562 assertEquals(0, list.getSelectionCount());
1563
1564 list.deselectAll();
1565 assertEquals(0, list.getSelectionCount());
1566
1567 list.select(0, 0);
1568 assertArrayEquals(list.getSelectionIndices(), new int[] { 0 });
1569
1570 list.select(1, 1);
1571 assertArrayEquals(list.getSelectionIndices(), new int[] { 1 });
1572
1573 list.select(2, 2);
1574 assertArrayEquals(list.getSelectionIndices(), new int[] { 2 });
1575
1576 list.select(3, 3);
1577 assertArrayEquals(list.getSelectionIndices(), new int[] { 3 });
1578
1579 list.select(4, 4);
1580 assertArrayEquals(list.getSelectionIndices(), new int[] { 4 });
1581
1582 list.select(5, 5);
1583 assertArrayEquals(list.getSelectionIndices(), new int[] { 4 });
1584
1585 list.deselectAll();
1586 list.select(5, 5);
1587 assertArrayEquals(list.getSelectionIndices(), new int[] {});
1588
1589 list.select(1, 2);
1590 assertArrayEquals(list.getSelectionIndices(), new int[] {});
1591
1592 list.select(1, 3);
1593 assertArrayEquals(list.getSelectionIndices(), new int[] {});
1594
1595 list.select(3, 5);
1596 assertArrayEquals(list.getSelectionIndices(), new int[] {});
1597
1598 selectII_helper(items, 0, 0, new int[]{0});
1599 selectII_helper(items, 1, 1, new int[]{1});
1600 selectII_helper(items, 2, 2, new int[]{2});
1601 selectII_helper(items, 3, 3, new int[]{3});
1602 selectII_helper(items, 4, 4, new int[]{4});
1603 selectII_helper(items, 5, 5, new int[]{});
1604 selectII_helper(items, 10, 1, new int[]{});
1605 selectII_helper(items, 3, 1, new int[]{});
1606 selectII_helper(items, -1, -1, new int[]{});
1607 }
1608
1609 @Override
1610 @Test
test_setFontLorg_eclipse_swt_graphics_Font()1611 public void test_setFontLorg_eclipse_swt_graphics_Font() {
1612 if (SwtTestUtil.isGTK) {
1613 //TODO Fix GTK failure.
1614 if (SwtTestUtil.verbose) {
1615 System.out.println("Excluded test_setFontLorg_eclipse_swt_graphics_Font(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_widgets_List)");
1616 }
1617 return;
1618 }
1619 FontData fontData = list.getFont().getFontData()[0];
1620 int lineHeight;
1621 Font font;
1622
1623 font = new Font(list.getDisplay(), fontData.getName(), 8, fontData.getStyle());
1624 list.setFont(font);
1625 lineHeight = list.getItemHeight();
1626 list.setFont(null);
1627 font.dispose();
1628 font = new Font(list.getDisplay(), fontData.getName(), 12, fontData.getStyle());
1629 list.setFont(font);
1630 assertEquals(font, list.getFont());
1631 assertTrue("itemHeight=" + list.getItemHeight() + ", lineHeight=" + lineHeight, list.getItemHeight() > lineHeight);
1632 list.setFont(null);
1633 font.dispose();
1634 }
1635
1636 @Test
test_setItemILjava_lang_String()1637 public void test_setItemILjava_lang_String() {
1638 String[] items = { "item0", "item1", "item2", "item3" };
1639
1640 assertEquals(list.getItemCount(), 0);
1641 int[] cases = { -10, 0, 10 };
1642 for (int index : cases) {
1643 try {
1644 list.setItem(index, null);
1645 fail("No exception thrown");
1646 } catch (IllegalArgumentException e) {
1647 }
1648 }
1649 assertEquals(list.getItemCount(), 0);
1650
1651 for (int index : cases) {
1652 try {
1653 list.setItem(index, "");
1654 fail("No exception thrown");
1655 } catch (IllegalArgumentException e) {
1656 }
1657 }
1658 assertEquals(list.getItemCount(), 0);
1659
1660 int cases2[] = { 10, 15, 0 };
1661 for (int index : cases2) {
1662 try {
1663 list.setItem(index, "fred");
1664 fail("No exception thrown");
1665 } catch (IllegalArgumentException e) {
1666 }
1667 assertEquals(list.getItemCount(), 0);
1668 }
1669
1670 list.setItems(items);
1671 list.setItem(1, "new1");
1672 assertArrayEquals(new String[] { "item0", "new1", "item2", "item3" }, list.getItems());
1673
1674 setSingleList();
1675 assertEquals(0, list.getItemCount());
1676 for (int index : cases) {
1677 try {
1678 list.setItem(index, null);
1679 fail("No exception thrown");
1680 } catch (IllegalArgumentException e) {
1681 }
1682
1683 }
1684
1685
1686 setSingleList();
1687 for (int index : cases) {
1688 try {
1689 list.setItem(index, "");
1690 fail("No exception thrown");
1691 } catch (IllegalArgumentException e) {
1692 }
1693 }
1694
1695 assertEquals(0, list.getItemCount());
1696
1697
1698 setSingleList();
1699 for (int index : cases2) {
1700 try {
1701 list.setItem(index, "fred");
1702 fail("No exception thrown");
1703 } catch (IllegalArgumentException e) {
1704 }
1705
1706 assertEquals(0, list.getItemCount());
1707 }
1708
1709 list.setItems(items);
1710 list.setItem(1, "new1");
1711 assertArrayEquals(new String[] { "item0", "new1", "item2", "item3" }, list.getItems());
1712
1713 }
1714
1715 @Test
test_setItems$Ljava_lang_String()1716 public void test_setItems$Ljava_lang_String() {
1717 try {
1718 list.setItems((String[])null);
1719 fail("No exception thrown for items == null");
1720 } catch (IllegalArgumentException e) {
1721 }
1722
1723 // TODO An SWTError should never happen and should not
1724 // be part of the test case. List should throw an
1725 // SWTException.
1726 // String[][] cases = { { null }, {
1727 // "dsada", null, "dsdasdasd" }
1728 // };
1729 // for (int i = 0; i < cases.length; i++) {
1730 // try {
1731 // list.setItems(cases[i]);
1732 // fail("No exception thrown for items not found");
1733 // } catch (SWTError e) {
1734 // }
1735 // }
1736
1737 String[][] itemArr = { {
1738 }, {
1739 "" }, {
1740 "sdasd" }, {
1741 "sdasd", "323434" }
1742 };
1743 for (String[] items : itemArr) {
1744 list.setItems(items);
1745 assertArrayEquals(items, list.getItems());
1746 }
1747
1748 try {
1749 list.setItems((String[])null);
1750 fail("No exception thrown");
1751 } catch (IllegalArgumentException e) {
1752 }
1753
1754
1755 setSingleList();
1756 for (int i = 0; i < itemArr.length; i++) {
1757 list.setItems(itemArr[i]);
1758 assertArrayEquals("case:" + i, itemArr[i], list.getItems());
1759 }
1760
1761
1762 try {
1763 list.setItems((String[])null);
1764 fail("No exception thrown");
1765 } catch (IllegalArgumentException e) {
1766 }
1767 }
1768
1769 @Test
test_setSelection$I()1770 public void test_setSelection$I() {
1771 int number = 8;
1772 for (int i = 0; i < number; i++)
1773 list.add("fred" + i);
1774
1775 list.setSelection(new int [0]);
1776 assertArrayEquals("MULTI: setSelection(new int [0])", list.getSelectionIndices(), new int[0]);
1777
1778 try {
1779 list.setSelection((int[]) null);
1780 fail("No exception thrown for MULTI: setSelection((int[]) null)");
1781 } catch (IllegalArgumentException e) {
1782 }
1783
1784 list.setSelection(new int [] {2});
1785 assertArrayEquals("MULTI: setSelection(new int [] {2})", list.getSelectionIndices(), new int[] {2});
1786 assertEquals("MULTI: setSelection(new int [] {2}) getFocusIndex()", list.getFocusIndex(), 2);
1787
1788 list.setSelection(new int [] {number});
1789 assertArrayEquals("MULTI: setSelection(new int [] {number})", list.getSelectionIndices(), new int[0]);
1790
1791 list.setSelection(new int [] {3, 1, 5, 2});
1792 assertArrayEquals("MULTI: setSelection(new int [] {3, 1, 5, 2})", list.getSelectionIndices(), new int[] {1, 2, 3, 5});
1793
1794 list.setSelection(new int [] {1, 0});
1795 assertArrayEquals("MULTI: setSelection(new int [] {1, 0})", list.getSelectionIndices(), new int[] {0, 1});
1796
1797 list.setSelection(new int [] {-1, number});
1798 assertArrayEquals("MULTI: setSelection(new int [] {-1, number})", list.getSelectionIndices(), new int[0]);
1799
1800 list.setSelection(new int [] {number - 1, number});
1801 assertArrayEquals("MULTI: setSelection(new int [] {number - 1, number})", list.getSelectionIndices(), new int[] {number - 1});
1802 assertEquals("MULTI: setSelection(new int [] {number - 1, number}) getFocusIndex()", list.getFocusIndex(), number - 1);
1803
1804 list.setSelection(new int [] {-1, 0});
1805 assertArrayEquals("MULTI: setSelection(new int [] {-1, 0})", list.getSelectionIndices(), new int[] {0});
1806
1807 list.setSelection(new int [] {0, 1, 2, 3, 5});
1808 assertArrayEquals("MULTI: setSelection(new int [] {0, 1, 2, 3, 5})", list.getSelectionIndices(), new int [] {0, 1, 2, 3, 5});
1809
1810 int[] indices = new int [number];
1811 for (int i = 0; i < number; i++) {
1812 indices[i] = i;
1813 }
1814 list.setSelection(indices);
1815 assertArrayEquals("MULTI: setSelection(indices)", indices, list.getSelectionIndices());
1816
1817 list.setSelection(new int [] {number, number});
1818 assertArrayEquals("MULTI: setSelection(new int [] {number, number})", new int[0], list.getSelectionIndices());
1819
1820 list.setSelection(new int [] {number - 1, number - 1});
1821 assertArrayEquals("MULTI: setSelection(new int [] {number - 1, number - 1})", list.getSelectionIndices(), new int[] {number - 1});
1822 assertEquals("MULTI: setSelection(new int [] {number - 1, number - 1}) getFocusIndex()", list.getFocusIndex(), number - 1);
1823
1824 list.setSelection(new int [] {0, number, 1});
1825 assertArrayEquals("MULTI: setSelection(new int [] {0, number, 1})", list.getSelectionIndices(), new int[] {0, 1});
1826
1827 list.setSelection(new int [] {number - 1, 0, number - 2});
1828 assertArrayEquals("MULTI: setSelection(new int [] {number - 1, 0, number - 2})", list.getSelectionIndices(), new int[] {0, number - 2, number - 1});
1829
1830 list.removeAll();
1831
1832 list.setSelection(new int [0]);
1833 assertArrayEquals("EMPTY MULTI: setSelection(new int [0])", list.getSelectionIndices(), new int[0]);
1834 assertEquals("EMPTY MULTI: setSelection(new int [0]) getFocusIndex()", list.getFocusIndex(), -1);
1835
1836 try {
1837 list.setSelection((int[]) null);
1838 fail("No exception thrown for EMPTY MULTI: setSelection((int[]) null)");
1839 } catch (IllegalArgumentException e) {
1840 }
1841
1842 list.setSelection(new int [] {0});
1843 assertArrayEquals("EMPTY MULTI: setSelection(new int [] {0})", list.getSelectionIndices(), new int[0]);
1844 assertEquals("EMPTY MULTI: setSelection(new int [] {0}) getFocusIndex()", list.getFocusIndex(), -1);
1845
1846 list.setSelection(new int [] {-1});
1847 assertArrayEquals("EMPTY MULTI: setSelection(new int [] {-1})", list.getSelectionIndices(), new int[0]);
1848 assertEquals("EMPTY MULTI: setSelection(new int [] {-1}) getFocusIndex()", list.getFocusIndex(), -1);
1849
1850 list.setSelection(new int [] {0, 0});
1851 assertArrayEquals("EMPTY MULTI: setSelection(new int [] {0, 0})", list.getSelectionIndices(), new int[0]);
1852 assertEquals("EMPTY MULTI: setSelection(new int [] {0, 0}) getFocusIndex()", list.getFocusIndex(), -1);
1853
1854 list.setSelection(new int [] {-1, 0});
1855 assertArrayEquals("EMPTY MULTI: setSelection(new int [] {-1, 0})", list.getSelectionIndices(), new int[0]);
1856 assertEquals("EMPTY MULTI: setSelection(new int [] {-1, 0}) getFocusIndex()", list.getFocusIndex(), -1);
1857
1858 list.setSelection(new int [] {0, -1});
1859 assertArrayEquals("EMPTY MULTI: setSelection(new int [] {0, -1})", list.getSelectionIndices(), new int[0]);
1860 assertEquals("EMPTY MULTI: setSelection(new int [] {0, -1}) getFocusIndex()", list.getFocusIndex(), -1);
1861
1862
1863 setSingleList();
1864 for (int i = 0; i < number; i++)
1865 list.add("fred" + i);
1866
1867 list.setSelection(new int [0]);
1868 assertArrayEquals("SINGLE: setSelection(new int [0])", list.getSelectionIndices(), new int[0]);
1869
1870 try {
1871 list.setSelection((int[]) null);
1872 fail("No exception thrown for SINGLE: setSelection((int[]) null)");
1873 } catch (IllegalArgumentException e) {
1874 }
1875
1876 list.setSelection(new int [] {2});
1877 assertArrayEquals("SINGLE: setSelection(new int [] {2})", list.getSelectionIndices(), new int[] {2});
1878 assertEquals("SINGLE: setSelection(new int [] {2}) getFocusIndex()", list.getFocusIndex(), 2);
1879
1880 list.setSelection(new int [] {number});
1881 assertArrayEquals("SINGLE: setSelection(new int [] {number})", list.getSelectionIndices(), new int[0]);
1882
1883 list.setSelection(new int [] {1, 0});
1884 assertArrayEquals("SINGLE: setSelection(new int [] {1, 0})", list.getSelectionIndices(), new int[] {});
1885
1886 list.setSelection(new int [] {0, 1, 2, 3, 5});
1887 assertArrayEquals("SINGLE: setSelection(new int [] {0, 1, 2, 3, 5})", list.getSelectionIndices(), new int [] {});
1888
1889 list.setSelection(new int [] {-1, number});
1890 assertArrayEquals("SINGLE: setSelection(new int [] {-1, number})", list.getSelectionIndices(), new int[0]);
1891
1892 list.setSelection(new int [] {number - 1, number});
1893 assertArrayEquals("SINGLE: setSelection(new int [] {number - 1, number})", list.getSelectionIndices(), new int[] {});
1894
1895 list.setSelection(new int [] {-1, 0});
1896 assertArrayEquals("SINGLE: setSelection(new int [] {-1, 0})", list.getSelectionIndices(), new int[] {});
1897
1898 indices = new int [number];
1899 for (int i = 0; i < number; i++) {
1900 indices[i] = i;
1901 }
1902 list.setSelection(indices);
1903 assertArrayEquals("SINGLE: setSelection(indices)", list.getSelectionIndices(), new int[] {});
1904
1905 list.setSelection(new int [] {number, number});
1906 assertArrayEquals("SINGLE: setSelection(new int [] {number, number})", list.getSelectionIndices(), new int[0]);
1907
1908 list.setSelection(new int [] {number - 1, number - 1});
1909 assertArrayEquals("SINGLE: setSelection(new int [] {number - 1, number - 1})", list.getSelectionIndices(), new int[] {});
1910
1911 list.setSelection(new int [] {0, number, 1});
1912 assertArrayEquals("SINGLE: setSelection(new int [] {0, number, 1})", list.getSelectionIndices(), new int[] {});
1913
1914 list.setSelection(new int [] {number - 1, 0, number - 2});
1915 assertArrayEquals("SINGLE: setSelection(new int [] {number - 1, 0, number - 2})", list.getSelectionIndices(), new int[] {});
1916
1917 list.removeAll();
1918
1919 list.setSelection(new int [0]);
1920 assertArrayEquals("EMPTY SINGLE: setSelection(new int [0])", list.getSelectionIndices(), new int[0]);
1921 assertEquals("EMPTY SINGLE: setSelection(new int [0]) getFocusIndex()", list.getFocusIndex(), -1);
1922
1923 try {
1924 list.setSelection((int[]) null);
1925 fail("No exception thrown for EMPTY SINGLE: setSelection((int[]) null)");
1926 } catch (IllegalArgumentException e) {
1927 }
1928
1929 list.setSelection(new int [] {0});
1930 assertArrayEquals("EMPTY SINGLE: setSelection(new int [] {0})", list.getSelectionIndices(), new int[0]);
1931 assertEquals("EMPTY SINGLE: setSelection(new int [] {0}) getFocusIndex()", list.getFocusIndex(), -1);
1932
1933 list.setSelection(new int [] {-1});
1934 assertArrayEquals("EMPTY SINGLE: setSelection(new int [] {-1})", list.getSelectionIndices(), new int[0]);
1935 assertEquals("EMPTY SINGLE: setSelection(new int [] {-1}) getFocusIndex()", list.getFocusIndex(), -1);
1936
1937 list.setSelection(new int [] {0, 0});
1938 assertArrayEquals("EMPTY SINGLE: setSelection(new int [] {0, 0})", list.getSelectionIndices(), new int[0]);
1939 assertEquals("EMPTY SINGLE: setSelection(new int [] {0, 0}) getFocusIndex()", list.getFocusIndex(), -1);
1940
1941 list.setSelection(new int [] {-1, 0});
1942 assertArrayEquals("EMPTY SINGLE: setSelection(new int [] {-1, 0})", list.getSelectionIndices(), new int[0]);
1943 assertEquals("EMPTY SINGLE: setSelection(new int [] {-1, 0}) getFocusIndex()", list.getFocusIndex(), -1);
1944
1945 list.setSelection(new int [] {0, -1});
1946 assertArrayEquals("EMPTY SINGLE: setSelection(new int [] {0, -1})", list.getSelectionIndices(), new int[0]);
1947 assertEquals("EMPTY SINGLE: setSelection(new int [] {0, -1}) getFocusIndex()", list.getFocusIndex(), -1);
1948 }
1949
1950 @Test
test_setSelection$Ljava_lang_String()1951 public void test_setSelection$Ljava_lang_String() {
1952 int number = 8;
1953 for (int i = 0; i < number; i++)
1954 list.add("fred " + i);
1955
1956 list.setSelection(new String [0]);
1957 assertArrayEquals(list.getSelection(), new String[0]);
1958 if (SwtTestUtil.fCheckSWTPolicy) {
1959 assertEquals(list.getFocusIndex(), -1);
1960 }
1961
1962 try {
1963 list.setSelection((String[]) null);
1964 fail("No exception thrown");
1965 } catch (IllegalArgumentException e) {
1966 }
1967
1968 list.setSelection(new String [] {"fred 2"});
1969 assertArrayEquals(list.getSelection(), new String [] {"fred 2"});
1970 assertEquals(list.getFocusIndex(), 2);
1971
1972 list.setSelection(new String [] {"fred " + number});
1973 assertArrayEquals(list.getSelection(), new String [0]);
1974
1975 list.setSelection(new String [] {"fred 1", "fred 0"});
1976 assertArrayEquals(list.getSelection(), new String [] {"fred 0", "fred 1"});
1977
1978 list.setSelection(new String [] {"fred -1", "fred " + number});
1979 assertArrayEquals(list.getSelection(), new String [0]);
1980
1981 list.setSelection(new String [] {"fred " + (number - 1), "fred " + number});
1982 assertArrayEquals(list.getSelection(), new String [] {"fred " + (number - 1)});
1983
1984 list.setSelection(new String [] {"fred -1", "fred 0"});
1985 assertArrayEquals(list.getSelection(), new String [] {"fred 0"});
1986
1987 String[] items = new String [number];
1988 for (int i = 0; i < number; i++) {
1989 items[i] = "fred " + i;
1990 }
1991 list.setSelection(items);
1992 assertArrayEquals(list.getSelection(), items);
1993
1994 list.setSelection(new String [] {"fred " + number, "fred " + number});
1995 assertArrayEquals(list.getSelection(), new String [0]);
1996
1997 list.setSelection(new String [] {"fred " + (number - 1), "fred " + (number - 1)});
1998 assertArrayEquals(list.getSelection(), new String[] {"fred " + (number - 1)});
1999
2000 list.setSelection(new String [] {"fred 0", "fred " + number, "fred 1"});
2001 assertArrayEquals(list.getSelection(), new String[] {"fred 0", "fred 1"});
2002
2003 list.removeAll();
2004
2005 list.setSelection(new String [0]);
2006 assertArrayEquals(list.getSelection(), new String[0]);
2007 assertEquals(list.getFocusIndex(), -1);
2008
2009 try {
2010 list.setSelection((String[]) null);
2011 fail("No exception thrown");
2012 } catch (IllegalArgumentException e) {
2013 }
2014
2015 list.setSelection(new String [] {"fred 0"});
2016 assertArrayEquals(list.getSelection(), new String[0]);
2017 assertEquals(list.getFocusIndex(), -1);
2018
2019 list.setSelection(new String [] {"fred 0", "fred 0"});
2020 assertArrayEquals(list.getSelection(), new String[0]);
2021 assertEquals(list.getFocusIndex(), -1);
2022
2023
2024 setSingleList();
2025 for (int i = 0; i < number; i++)
2026 list.add("fred " + i);
2027
2028 list.setSelection(new String [0]);
2029 assertArrayEquals(list.getSelection(), new String[0]);
2030
2031 try {
2032 list.setSelection((String[]) null);
2033 fail("No exception thrown");
2034 } catch (IllegalArgumentException e) {
2035 }
2036
2037 list.setSelection(new String [] {"fred 2"});
2038 assertArrayEquals(list.getSelection(), new String[] {"fred 2"});
2039 assertEquals(list.getFocusIndex(), 2);
2040
2041 list.setSelection(new String [] {"fred " + number});
2042 assertArrayEquals(list.getSelection(), new String[0]);
2043
2044 list.setSelection(new String [] {"fred 1", "fred 0"});
2045 assertArrayEquals(list.getSelection(), new String[] {});
2046
2047 list.setSelection(new String [] {"fred -1", "fred " + number});
2048 assertArrayEquals(list.getSelection(), new String[0]);
2049
2050 list.setSelection(new String [] {"fred " + (number - 1)});
2051 assertArrayEquals(list.getSelection(), new String[] {"fred " + (number - 1)});
2052
2053 items = new String[number];
2054 for (int i = 0; i < number; i++) {
2055 items[i] = "fred " + i;
2056 }
2057 list.setSelection(items);
2058 assertArrayEquals(list.getSelection(), new String[] {});
2059
2060 list.setSelection(new String [] {"fred " + number, "fred " + number});
2061 assertArrayEquals(list.getSelection(), new String[0]);
2062
2063 list.setSelection(new String [] {"fred " + (number - 1), "fred " + (number - 1)});
2064 assertArrayEquals(list.getSelection(), new String[] {});
2065
2066 list.setSelection(new String [] {"fred 0", "fred " + number, "fred 1"});
2067 assertArrayEquals(list.getSelection(), new String[] {});
2068
2069 list.removeAll();
2070
2071 list.setSelection(new String [0]);
2072 assertArrayEquals(list.getSelection(), new String[0]);
2073 assertEquals(list.getFocusIndex(), -1);
2074
2075 try {
2076 list.setSelection((String[]) null);
2077 fail("No exception thrown");
2078 } catch (IllegalArgumentException e) {
2079 }
2080
2081 list.setSelection(new String [] {"fred 0"});
2082 assertArrayEquals(list.getSelection(), new String[0]);
2083 assertEquals(list.getFocusIndex(), -1);
2084
2085 list.setSelection(new String [] {"fred 0", "fred 0"});
2086 assertArrayEquals(list.getSelection(), new String[0]);
2087 assertEquals(list.getFocusIndex(), -1);
2088 }
2089
2090 @Test
test_setSelectionI()2091 public void test_setSelectionI() {
2092 int number = 8;
2093 for (int i = 0; i < number; i++) {
2094 list.add("fred" + i);
2095 }
2096
2097 list.setSelection(2);
2098 assertArrayEquals(list.getSelectionIndices(), new int[] {2});
2099 assertEquals(list.getFocusIndex(), 2);
2100
2101 list.setSelection(-5);
2102 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2103
2104 list.setSelection(0);
2105 assertArrayEquals(list.getSelectionIndices(), new int[] {0});
2106 assertEquals(list.getFocusIndex(), 0);
2107
2108 list.setSelection(number);
2109 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2110
2111 list.setSelection(number - 1);
2112 assertArrayEquals(list.getSelectionIndices(), new int[] {number - 1});
2113
2114 list.setSelection(-1);
2115 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2116
2117 list.removeAll();
2118
2119 list.setSelection(-2);
2120 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2121 assertEquals(list.getFocusIndex(), -1);
2122
2123 list.setSelection(0);
2124 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2125 assertEquals(list.getFocusIndex(), -1);
2126
2127 list.setSelection(-1);
2128 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2129 assertEquals(list.getFocusIndex(), -1);
2130
2131 setSingleList();
2132
2133 for (int i = 0; i < number; i++) {
2134 list.add("fred" + i);
2135 }
2136
2137 list.setSelection(2);
2138 assertArrayEquals(list.getSelectionIndices(), new int[] {2});
2139 assertEquals(list.getFocusIndex(), 2);
2140
2141 list.setSelection(-5);
2142 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2143
2144 list.setSelection(0);
2145 assertArrayEquals(list.getSelectionIndices(), new int[] {0});
2146 assertEquals(list.getFocusIndex(), 0);
2147
2148 list.setSelection(number);
2149 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2150
2151 list.setSelection(number - 1);
2152 assertArrayEquals(list.getSelectionIndices(), new int[] {number - 1});
2153 assertEquals(list.getFocusIndex(), number - 1);
2154
2155 list.setSelection(-1);
2156 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2157
2158 list.removeAll();
2159
2160 list.setSelection(0);
2161 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2162 assertEquals(list.getFocusIndex(), -1);
2163
2164 list.setSelection(-1);
2165 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2166 assertEquals(list.getFocusIndex(), -1);
2167
2168 list.setSelection(-2);
2169 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2170 assertEquals(list.getFocusIndex(), -1);
2171 }
2172
2173 @Test
test_setSelectionII()2174 public void test_setSelectionII() {
2175 int number = 8;
2176 String[] items = new String[number];
2177 for (int i = 0; i < number; i++)
2178 items[i] = "fred" + i;
2179
2180 list.setItems(items);
2181
2182 list.setSelection(1, 2);
2183 assertArrayEquals(list.getSelectionIndices(), new int[] {1, 2});
2184
2185 list.setSelection(-3, -2);
2186 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2187
2188 list.setSelection(0, 1);
2189 assertArrayEquals(list.getSelectionIndices(), new int[] {0, 1});
2190
2191 list.setSelection(-2, -1);
2192 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2193
2194 list.setSelection(number - 2, number - 1);
2195 assertArrayEquals(list.getSelectionIndices(), new int[] {number - 2, number - 1});
2196
2197 list.setSelection(number - 1, number);
2198 assertArrayEquals(list.getSelectionIndices(), new int[] {number - 1});
2199
2200 list.setSelection(-1, 0);
2201 assertArrayEquals(list.getSelectionIndices(), new int[] {0});
2202
2203 list.setSelection(number, number + 1);
2204 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2205
2206 list.setSelection(0, 0);
2207 assertArrayEquals(list.getSelectionIndices(), new int[] {0});
2208 assertEquals(list.getFocusIndex(), 0);
2209
2210 list.setSelection(2, 1);
2211 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2212
2213 list.setSelection(number - 1, number - 1);
2214 assertArrayEquals(list.getSelectionIndices(), new int[] {number - 1});
2215 assertEquals(list.getFocusIndex(), number - 1);
2216
2217 list.setSelection(-1, -1);
2218 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2219
2220 list.removeAll();
2221
2222 list.setSelection(-2, -1);
2223 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2224 assertEquals(list.getFocusIndex(), -1);
2225
2226 list.setSelection(-1, 0);
2227 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2228 assertEquals(list.getFocusIndex(), -1);
2229
2230 list.setSelection(0, 1);
2231 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2232 assertEquals(list.getFocusIndex(), -1);
2233
2234 list.setSelection(1, 0);
2235 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2236 assertEquals(list.getFocusIndex(), -1);
2237
2238 list.setSelection(0, -1);
2239 assertArrayEquals(list.getSelectionIndices(), new int[0]);
2240 assertEquals(list.getFocusIndex(), -1);
2241
2242
2243 setSingleList();
2244 list.setItems(items);
2245
2246 list.setSelection(0, 0);
2247 assertArrayEquals(list.getSelectionIndices(), new int[] {0});
2248 assertEquals(list.getFocusIndex(), 0);
2249
2250 list.setSelection(1, 1);
2251 assertArrayEquals(list.getSelectionIndices(), new int[] {1});
2252 assertEquals(list.getFocusIndex(), 1);
2253
2254 list.setSelection(4, 4);
2255 assertArrayEquals(list.getSelectionIndices(), new int[] {4});
2256 assertEquals(list.getFocusIndex(), 4);
2257
2258 list.setSelection(number - 1, number - 1);
2259 assertArrayEquals(list.getSelectionIndices(), new int[] {number - 1});
2260 assertEquals(list.getFocusIndex(), number - 1);
2261
2262 list.setSelection(number, number);
2263 assertArrayEquals(list.getSelectionIndices(), new int[] {});
2264
2265 list.setSelection(-3, -2);
2266 assertArrayEquals(list.getSelectionIndices(), new int[] {});
2267
2268 list.setSelection(0, 1);
2269 assertArrayEquals(list.getSelectionIndices(), new int[] {});
2270
2271 list.setSelection(-2, -1);
2272 assertArrayEquals(list.getSelectionIndices(), new int[] {});
2273
2274 list.setSelection(number - 2, number - 1);
2275 assertArrayEquals(list.getSelectionIndices(), new int[] {});
2276
2277 list.setSelection(number - 1, number);
2278 assertArrayEquals(list.getSelectionIndices(), new int[] {});
2279
2280 list.setSelection(-1, 0);
2281 assertArrayEquals(list.getSelectionIndices(), new int[] {});
2282
2283 list.setSelection(number, number + 1);
2284 assertArrayEquals(list.getSelectionIndices(), new int[] {});
2285
2286 list.setSelection(2, 1);
2287 assertArrayEquals(list.getSelectionIndices(), new int[] {});
2288
2289 list.setSelection(number - 1, number - 1);
2290 assertArrayEquals(list.getSelectionIndices(), new int[] {number - 1});
2291 assertEquals(list.getFocusIndex(), number - 1);
2292
2293 list.setSelection(-1, -1);
2294 assertArrayEquals(list.getSelectionIndices(), new int[] {});
2295 }
2296
2297 @Test
test_setTopIndexI()2298 public void test_setTopIndexI() {
2299 if (SwtTestUtil.isGTK) {
2300 //TODO Fix GTK failure.
2301 if (SwtTestUtil.verbose) {
2302 System.out.println("Excluded test_setTopIndexI(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_widgets_List)");
2303 }
2304 return;
2305 }
2306 list.setTopIndex(3);
2307 assertEquals("MULTI: setTopIndex(3) in empty list", 0, list.getTopIndex());
2308 String[] items = { "item0", "item1", "item2", "item3" };
2309 list.setItems(items);
2310 for (int i = 0; i < items.length; i++) {
2311 list.setTopIndex(i);
2312 assertEquals("MULTI: setTopIndex(i=" + i + ")", i, list.getTopIndex());
2313 }
2314
2315
2316 setSingleList();
2317 list.setTopIndex(3);
2318 assertEquals("SINGLE: setTopIndex(3) in empty list", 0, list.getTopIndex());
2319
2320 list.setItems(items);
2321 for (int i = 0; i < items.length; i++) {
2322 list.setTopIndex(i);
2323 assertEquals("SINGLE: setTopIndex(i=" + i + ")", i, list.getTopIndex());
2324 }
2325
2326 }
2327
2328 @Test
test_showSelection()2329 public void test_showSelection() {
2330 String[] items = { "item0", "item1", "item2", "item3" };
2331 list.setItems(items);
2332 list.setSelection(items);
2333 list.showSelection();
2334
2335
2336 setSingleList();
2337
2338 list.setItems(items);
2339 list.setSelection(items);
2340 list.showSelection();
2341 }
2342
2343 /* custom */
2344 List list;
2345 /**
2346 * Test if 'deselect(u, v)' is the same as 'for (i=u; i<=v; ++i) deselect(i);'
2347 */
deselectII_helper( String[] items, int start, int end, int[] expectedIndices)2348 protected void deselectII_helper(
2349 String[] items,
2350 int start,
2351 int end,
2352 int[] expectedIndices) {
2353
2354 list.setItems(items);
2355 list.setSelection(items);
2356
2357 list.deselect(start, end);
2358 assertArrayEquals(
2359 ":(" + start + ", " + end + "):",
2360 expectedIndices, list.getSelectionIndices());
2361
2362 list.setSelection(items);
2363 if ( 0 != (list.getStyle() & SWT.MULTI) ) {
2364 assertArrayEquals("setSelection(items):", items, list.getSelection());
2365 }
2366
2367 for (int i = start; i <= end; ++i) {
2368 list.deselect(i);
2369 }
2370 assertArrayEquals(
2371 ":(" + start + ", " + end + "):",
2372 expectedIndices, list.getSelectionIndices());
2373
2374 list.deselectAll();
2375 }
2376 /**
2377 * Dispose of the main list and create a new, single-selection one.
2378 */
setSingleList()2379 protected List setSingleList() {
2380 list.dispose();
2381 list = new List(shell, SWT.SINGLE);
2382 setWidget(list);
2383 return list;
2384 }
2385 /**
2386 * Similar to deselectII_helper, checks if select(u, v) is the same as
2387 * for (i=u; i<=v; ++i) select(i)
2388 */
selectII_helper( String[] items, int start, int end, int[] expectedIndices)2389 protected void selectII_helper(
2390 String[] items,
2391 int start,
2392 int end,
2393 int[] expectedIndices) {
2394 list.setItems(items);
2395 list.select(start, end);
2396 assertArrayEquals(
2397 ":(" + start + ", " + end + "):",
2398 expectedIndices, list.getSelectionIndices());
2399
2400 list.deselectAll();
2401 assertArrayEquals("deselectAll:", list.getSelectionIndices(), new int[] {});
2402
2403 for (int i = start; i <= end; i++) // <= on purpose
2404 list.select(i);
2405
2406 assertArrayEquals(":(" + start + ", " + end + "):",
2407 expectedIndices, list.getSelectionIndices());
2408
2409 list.deselectAll();
2410 }
2411 /**
2412 * Similar to deselectII_helper, checks if select(int []arr) gives the same
2413 * result as several individual select(int) calls. The int[] used for selection
2414 * will be filled all integers from start to end inclusive, in order.
2415 */
select$I_helper( int start, int end, int[] expectedIndices)2416 protected void select$I_helper(
2417 int start,
2418 int end,
2419 int[] expectedIndices) {
2420 int[] selection = new int[end - start + 1];
2421 for (int i = 0; i < selection.length; ++i) {
2422 selection[i] = i + start;
2423 }
2424
2425 list.select(selection);
2426
2427 assertArrayEquals(
2428 ":(" + start + ", " + end + "):",
2429 expectedIndices, list.getSelectionIndices());
2430
2431 list.deselectAll();
2432 assertArrayEquals("deselectAll:", list.getSelectionIndices(), new int[] {});
2433
2434 for (int i = start; i <= end; i++) // <= on purpose
2435 list.select(i);
2436
2437 assertArrayEquals(
2438 ":(" + start + ", " + end + "):",
2439 expectedIndices, list.getSelectionIndices());
2440
2441 list.deselectAll();
2442 }
2443
add()2444 private void add() {
2445 list.add("this");
2446 list.add("is");
2447 list.add("SWT");
2448 }
2449
2450 @Test
test_consistency_MouseSelection()2451 public void test_consistency_MouseSelection () {
2452 add();
2453 consistencyEvent(27, 10, 1, 0, ConsistencyUtility.MOUSE_CLICK);
2454 }
2455
2456 @Test
test_consistency_KeySelection()2457 public void test_consistency_KeySelection () {
2458 add();
2459 consistencyEvent(0, SWT.ARROW_DOWN, 0, 0, ConsistencyUtility.KEY_PRESS);
2460 }
2461
2462 @Test
test_consistency_SpaceSelection()2463 public void test_consistency_SpaceSelection () {
2464 add();
2465 consistencyEvent(' ', 32, 0, 0, ConsistencyUtility.KEY_PRESS);
2466 }
2467
2468 @Test
test_consistency_DoubleClick()2469 public void test_consistency_DoubleClick () {
2470 add();
2471 consistencyEvent(27, 10, 1, 0, ConsistencyUtility.MOUSE_DOUBLECLICK);
2472 }
2473
2474 @Test
test_consistency_MenuDetect()2475 public void test_consistency_MenuDetect () {
2476 add();
2477 consistencyEvent(27, 5, 3, 0, ConsistencyUtility.MOUSE_CLICK);
2478 }
2479 @Test
test_consistency_DragDetect()2480 public void test_consistency_DragDetect () {
2481 add();
2482 consistencyEvent(20, 5, 30, 10, ConsistencyUtility.MOUSE_DRAG);
2483 }
2484 }
2485