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 Dmitry A. Durnev
019: * @version $Revision$
020: */package java.awt;
021:
022: import java.awt.event.*;
023:
024: import junit.framework.TestCase;
025:
026: /**
027: * ListTest
028: */
029: public class ListTest extends TestCase {
030: private List list;
031: private boolean eventProcessed;
032:
033: @Override
034: protected void setUp() throws Exception {
035: super .setUp();
036: list = new List();
037: eventProcessed = false;
038: }
039:
040: private void selectCurrentItem() {
041: list.setMultipleMode(true);
042: for (int i = 0; i < list.getItemCount(); i++) {
043: list.select(i);
044: }
045: list.setMultipleMode(false);
046: }
047:
048: public final void testAddNotify() {
049: Frame f = new Frame("");
050: f.add(list);
051: assertNull(list.getGraphics());
052: list.add("item");
053: list.add("item1", 0);
054: selectCurrentItem();
055: assertEquals(-1, list.getSelectedIndex());
056: f.addNotify();
057: selectCurrentItem();
058: assertEquals(list.getItemCount() - 1, list.getSelectedIndex());
059: assertNotNull(list.getGraphics());
060: f.dispose();
061: assertNull(list.getGraphics());
062: }
063:
064: public final void testGetAccessibleContext() {
065: //TODO Implement getAccessibleContext().
066: }
067:
068: public final void testParamString() {
069: String str = list.paramString();
070: assertEquals("name is correct", 0, str.indexOf("list"));
071: assertTrue(str.indexOf("selected=" + null) > 0);
072: }
073:
074: public final void testRemoveNotify() {
075: Frame f = new Frame("");
076: f.add(list);
077: list.add("item");
078: list.add("item1", 0);
079: f.addNotify();
080: assertNotNull(list.getGraphics());
081: selectCurrentItem();
082: assertEquals(list.getItemCount() - 1, list.getSelectedIndex());
083: list.removeNotify();
084: selectCurrentItem();
085: assertEquals(-1, list.getSelectedIndex());
086: assertNull(list.getGraphics());
087: f.dispose();
088: }
089:
090: /*
091: * Class under test for java.awt.Dimension getMinimumSize()
092: */
093: public final void testGetMinimumSize() {
094: assertEquals(new Dimension(), list.getMinimumSize());
095: Frame f = new Frame("");
096: f.add(list);
097: f.addNotify();
098: Dimension minSize = list.getMinimumSize();
099: int fontSize = list.getFont().getSize();
100: assertTrue("min height > font size * rows",
101: minSize.height > list.getRows() * fontSize);
102: assertTrue("min width > 0", minSize.width > 0);
103: f.dispose();
104: }
105:
106: /*
107: * Class under test for java.awt.Dimension minimumSize()
108: */
109: public final void testMinimumSize() {
110: // TODO: deprecated
111: }
112:
113: /*
114: * Class under test for java.awt.Dimension preferredSize()
115: */
116: public final void testPreferredSize() {
117: // TODO: deprecated
118: }
119:
120: public final void testProcessEvent() {
121: eventProcessed = false;
122: list.addKeyListener(new KeyAdapter() {
123: @Override
124: public void keyPressed(KeyEvent a0) {
125: eventProcessed = true;
126: }
127: });
128: list.processEvent(new KeyEvent(list, KeyEvent.KEY_PRESSED, 0,
129: 0, 0, 'q'));
130: assertTrue(eventProcessed);
131: }
132:
133: public final void testGetListeners() {
134: assertEquals(0, list.getListeners(KeyListener.class).length);
135: KeyAdapter listener = new KeyAdapter() {
136: };
137: list.addKeyListener(listener);
138: Class<KeyListener> clazz = KeyListener.class;
139: assertEquals(1, list.getListeners(clazz).length);
140: assertEquals(listener, list.getListeners(clazz)[0]);
141: list.removeKeyListener(listener);
142: assertEquals(0, list.getListeners(clazz).length);
143: }
144:
145: /*
146: * Class under test for void List(int)
147: */
148: public final void testListint() {
149: int rows = 0;
150: list = new List(rows);
151: assertEquals(4, list.getRows());
152: assertFalse(list.isMultipleMode());
153: list = new List(rows = 1);
154: assertEquals(rows, list.getRows());
155: list = new List(rows = Integer.MAX_VALUE);
156: assertEquals(rows, list.getRows());
157: list = new List(rows = Integer.MIN_VALUE);
158: assertEquals(rows, list.getRows());
159:
160: }
161:
162: /*
163: * Class under test for void List()
164: */
165: public final void testList() {
166: assertEquals(4, list.getRows());
167: assertEquals(-1, list.getSelectedIndex());
168: assertNull(list.getSelectedItem());
169: assertFalse(list.isMultipleMode());
170: assertEquals(0, list.getSelectedItems().length);
171: assertEquals(0, list.getSelectedIndexes().length);
172: assertEquals(0, list.getSelectedObjects().length);
173: }
174:
175: /*
176: * Class under test for void List(int, boolean)
177: */
178: public final void testListintboolean() {
179: int rows = 0;
180: list = new List(rows, false);
181: assertEquals(4, list.getRows());
182: assertFalse(list.isMultipleMode());
183: list = new List(rows = 13, true);
184: assertEquals(rows, list.getRows());
185: assertTrue(list.isMultipleMode());
186: }
187:
188: /*
189: * Class under test for void add(java.lang.String)
190: */
191: public final void testAddString() {
192: String item = "item";
193: list.add(item);
194: assertSame(item, list.getItem(0));
195: list.add(item = "item1");
196: assertSame(item, list.getItem(1));
197: list.add(item = null);
198: assertEquals("", list.getItem(2));
199:
200: }
201:
202: /*
203: * Class under test for void add(java.lang.String, int)
204: */
205: public final void testAddStringint() {
206: String item = "item";
207: int idx = 0;
208: list.add(item, idx);
209: assertSame(item, list.getItem(idx));
210: list.add(item = "item1", idx);
211: assertEquals("item", list.getItem(idx + 1));
212: assertSame(item, list.getItem(idx));
213: list.add(item = "item2", idx = -20);
214: assertSame(item, list.getItem(list.getItemCount() - 1));
215: list.add(item = "item3", idx = Integer.MAX_VALUE);
216: assertSame(item, list.getItem(list.getItemCount() - 1));
217: list.add(item = null, idx = 1);
218: assertEquals("", list.getItem(idx));
219:
220: }
221:
222: /*
223: * Class under test for void remove(java.lang.String)
224: */
225: public final void testRemoveString() {
226: String item = "q";
227: boolean iae = false;
228: try {
229: list.remove(item);
230: } catch (IllegalArgumentException e) {
231: iae = true;
232: }
233: assertTrue(iae);
234: list.add(item);
235: list.add(item);
236: list.remove(item);
237: assertEquals(1, list.getItemCount());
238: list.select(0);
239: assertSame(item, list.getSelectedItem());
240: list.remove(item);
241: assertEquals(0, list.getItemCount());
242: assertNull(list.getSelectedItem());
243: assertEquals(-1, list.getSelectedIndex());
244: }
245:
246: /*
247: * Class under test for void remove(int)
248: */
249: public final void testRemoveint() {
250: int pos = 0;
251: boolean exception = false;
252: try {
253: list.remove(pos);
254: } catch (ArrayIndexOutOfBoundsException e) {
255: exception = true;
256: }
257: assertTrue(exception);
258: String item = "item";
259: list.add(item);
260: list.add(item);
261: list.remove(1);
262: assertEquals(1, list.getItemCount());
263: assertSame(item, list.getItem(0));
264: list.select(0);
265: assertSame(item, list.getSelectedItem());
266: list.remove(0);
267: assertEquals(0, list.getItemCount());
268: assertEquals(-1, list.getSelectedIndex());
269: assertNull(list.getSelectedItem());
270: list.add(item = "item1");
271: list.add("item2");
272: list.add("item3");
273: list.setMultipleMode(true);
274: list.select(0);
275: list.select(2);
276: assertEquals(2, list.getSelectedIndexes()[1]);
277: list.remove(1);
278: // test shift of selected indexes:
279: assertEquals(1, list.getSelectedIndexes()[1]);
280:
281: }
282:
283: public final void testClear() {
284: // TODO: deprecated
285: }
286:
287: public final void testRemoveAll() {
288: list.add("ppp");
289: list.add("qqq");
290: list.removeAll();
291: assertEquals(0, list.getItemCount());
292: assertEquals(0, list.getItems().length);
293: }
294:
295: /*
296: * Class under test for java.awt.Dimension getMinimumSize(int)
297: */
298: public final void testGetMinimumSizeint() {
299: int i = 1;
300: assertEquals(new Dimension(), list.getMinimumSize(i));
301: Frame f = new Frame("");
302: f.add(list);
303: f.addNotify();
304: Dimension minSize = list.getMinimumSize(i);
305: int fontSize = list.getFont().getSize();
306: assertTrue("min height > font size * i", minSize.height > i
307: * fontSize);
308: assertTrue("min width > 0", minSize.width > 0);
309: f.dispose();
310: }
311:
312: /*
313: * Class under test for java.awt.Dimension minimumSize(int)
314: */
315: public final void testMinimumSizeint() {
316: // TODO: deprecated
317: }
318:
319: /*
320: * Class under test for java.awt.Dimension preferredSize(int)
321: */
322: public final void testPreferredSizeint() {
323: // TODO: deprecated
324: }
325:
326: /*
327: * Class under test for void addItem(java.lang.String)
328: */
329: public final void testAddItemString() {
330: // TODO: deprecated
331: }
332:
333: /*
334: * Class under test for void addItem(java.lang.String, int)
335: */
336: public final void testAddItemStringint() {
337: // TODO: deprecated
338: }
339:
340: public final void testAllowsMultipleSelections() {
341: // TODO: deprecated
342: }
343:
344: public final void testCountItems() {
345: // TODO: deprecated
346: }
347:
348: public final void testDelItem() {
349: // TODO: deprecated
350: }
351:
352: @SuppressWarnings("deprecation")
353: public final void testDelItems() {
354: String item = "item";
355: list.add(item);
356: list.add("item1");
357: list.add("item2");
358: list.delItems(1, 2);
359: assertEquals(1, list.getItemCount());
360: assertSame(item, list.getItem(0));
361: }
362:
363: public final void testDeselect() {
364: list.add("item");
365: list.add("item1");
366: list.add("item2");
367: int idx = 0;
368: list.deselect(idx);
369: assertEquals(-1, list.getSelectedIndex());
370: list.select(idx);
371: assertEquals(idx, list.getSelectedIndex());
372: list.deselect(idx);
373: assertEquals(-1, list.getSelectedIndex());
374: list.select(2);
375: list.setMultipleMode(true);
376: list.select(idx);
377: list.deselect(2);
378: assertEquals(idx, list.getSelectedIndex());
379: assertEquals(idx, list.getSelectedIndexes()[0]);
380: }
381:
382: public final void testGetItemCount() {
383: assertEquals(0, list.getItemCount());
384: }
385:
386: public final void testGetItems() {
387: assertEquals(0, list.getItems().length);
388: }
389:
390: public final void testGetRows() {
391: assertEquals(4, list.getRows());
392: }
393:
394: public final void testGetSelectedIndex() {
395: assertEquals(-1, list.getSelectedIndex());
396: list = new List(5, true);
397: list.add("qqq");
398: assertEquals(-1, list.getSelectedIndex());
399: list.select(0);
400: assertEquals(0, list.getSelectedIndex());
401: list.add("ppp");
402: list.select(1);
403: assertEquals(-1, list.getSelectedIndex());
404: }
405:
406: public final void testGetSelectedIndexes() {
407: assertEquals(0, list.getSelectedIndexes().length);
408: String item = "item";
409: list.add(item);
410: int idx = 0;
411: list.select(idx);
412: assertEquals(idx, list.getSelectedIndexes()[0]);
413: list.setMultipleMode(true);
414: list.add(item);
415: list.select(0);
416: assertEquals(idx, list.getSelectedIndexes()[0]);
417: list.add(item = "item1");
418: list.select(idx = 1);
419: assertEquals(idx, list.getSelectedIndexes()[1]);
420: }
421:
422: public final void testGetSelectedItem() {
423: assertNull(list.getSelectedItem());
424: }
425:
426: public final void testGetSelectedItems() {
427: assertEquals(0, list.getSelectedItems().length);
428: }
429:
430: public final void testGetSelectedObjects() {
431: assertEquals(0, list.getSelectedObjects().length);
432: }
433:
434: public final void testGetVisibleIndex() {
435: assertEquals(-1, list.getVisibleIndex());
436: }
437:
438: public final void testIsIndexSelected() {
439: assertFalse(list.isIndexSelected(-1));
440: assertFalse(list.isIndexSelected(0));
441: list.add("item");
442: list.add("item1");
443: list.add("item2");
444: list.select(0);
445: assertTrue(list.isIndexSelected(0));
446: assertFalse(list.isIndexSelected(1));
447: list.select(1);
448: assertTrue(list.isIndexSelected(1));
449: assertFalse(list.isIndexSelected(0));
450: list.setMultipleMode(true);
451: list.select(2);
452: assertTrue(list.isIndexSelected(1));
453: assertTrue(list.isIndexSelected(2));
454: assertFalse(list.isIndexSelected(0));
455: }
456:
457: public final void testIsMultipleMode() {
458: assertFalse(list.isMultipleMode());
459: }
460:
461: public final void testIsSelected() {
462: // TODO: deprecated
463: }
464:
465: public final void testMakeVisible() {
466: list.add("item1");
467: list.add("item2");
468: int idx = -1;
469: list.makeVisible(idx);
470: assertEquals(idx, list.getVisibleIndex());
471: list.makeVisible(idx = 1);
472: assertEquals(idx, list.getVisibleIndex());
473: }
474:
475: public final void testReplaceItem() {
476: int pos = 0;
477: boolean exception = false;
478: String item = "", item1 = "item1";
479: try {
480: list.replaceItem(item, pos);
481: } catch (ArrayIndexOutOfBoundsException e) {
482: exception = true;
483: }
484: assertTrue(exception);
485: list.add(item = "item");
486: list.replaceItem(item1, pos);
487: assertSame(item1, list.getItem(pos));
488: }
489:
490: public final void testSelect() {
491: list.add("item");
492: list.add("item1");
493: list.add("item2");
494: int idx = 0;
495: list.select(idx);
496: assertEquals(idx, list.getSelectedIndex());
497: list.select(idx = 1);
498: assertEquals(idx, list.getSelectedIndex());
499:
500: assertEquals(idx, list.getSelectedIndexes()[0]);
501: list.setMultipleMode(true);
502: assertEquals(idx, list.getSelectedIndex());
503: assertEquals(idx, list.getSelectedIndexes()[0]);
504: list.select(idx = 2);
505: assertEquals(-1, list.getSelectedIndex());
506: int[] indexes = list.getSelectedIndexes();
507: assertEquals(1, indexes[0]);
508: assertEquals(idx, indexes[1]);
509:
510: }
511:
512: // public final void testCrash() {
513: // System.out.println((new int[0])[0]);
514: // }
515:
516: public final void testSetMultipleMode() {
517: list.setMultipleMode(true);
518: assertTrue(list.isMultipleMode());
519: list.add("item");
520: list.add("item1");
521: list.select(1);
522: list.select(0);
523: Frame f = new Frame("");
524: f.add(list);
525: f.pack();
526: list.setMultipleMode(false);
527: assertFalse(list.isMultipleMode());
528: assertEquals(0, list.getSelectedIndex());
529: f.dispose();
530: }
531:
532: public final void testSetMultipleSelections() {
533: // TODO: deprecated
534: }
535:
536: public final void testAddGetRemoveActionListener() {
537: assertEquals(0, list.getActionListeners().length);
538:
539: ActionListener listener = new ActionListener() {
540: public void actionPerformed(ActionEvent e) {
541: }
542: };
543: list.addActionListener(listener);
544: assertEquals(1, list.getActionListeners().length);
545: assertSame(listener, list.getActionListeners()[0]);
546:
547: list.removeActionListener(listener);
548: assertEquals(0, list.getActionListeners().length);
549: }
550:
551: public final void testAddGetRemoveItemListener() {
552: assertEquals(0, list.getItemListeners().length);
553:
554: ItemListener listener = new ItemListener() {
555: public void itemStateChanged(ItemEvent ie) {
556: }
557: };
558:
559: list.addItemListener(listener);
560: assertEquals(1, list.getItemListeners().length);
561: assertSame(listener, list.getItemListeners()[0]);
562:
563: list.removeItemListener(listener);
564: assertEquals(0, list.getItemListeners().length);
565: }
566:
567: public final void testProcessItemEvent() {
568: eventProcessed = false;
569: list.addItemListener(new ItemListener() {
570: public void itemStateChanged(ItemEvent ie) {
571: eventProcessed = true;
572: }
573: });
574: list
575: .processEvent(new ItemEvent(list,
576: ItemEvent.ITEM_STATE_CHANGED, null,
577: ItemEvent.SELECTED));
578: assertTrue(eventProcessed);
579: }
580:
581: public final void testProcessActionEvent() {
582: eventProcessed = false;
583: list.addActionListener(new ActionListener() {
584: public void actionPerformed(ActionEvent e) {
585: eventProcessed = true;
586: }
587: });
588: list.processEvent(new ActionEvent(list,
589: ActionEvent.ACTION_PERFORMED, null, 0, 0));
590: assertTrue(eventProcessed);
591: }
592:
593: }
|