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.ItemEvent;
023: import java.awt.event.ItemListener;
024: import java.awt.event.KeyAdapter;
025: import java.awt.event.KeyEvent;
026: import java.awt.event.KeyListener;
027:
028: import junit.framework.TestCase;
029:
030: /**
031: * ChoiceTest
032: */
033: public class ChoiceTest extends TestCase {
034:
035: private Choice choice;
036: private boolean eventProcessed;
037:
038: @Override
039: protected void setUp() throws Exception {
040: super .setUp();
041: choice = new Choice();
042: eventProcessed = false;
043: }
044:
045: public final void testAddNotify() {
046: Frame f = new Frame("");
047: f.add(choice);
048: assertNull(choice.getGraphics());
049: assertEquals(new Dimension(), choice.getMinimumSize());
050: choice.add("item");
051: assertEquals(0, choice.getSelectedIndex());
052: choice.add("item1");
053: f.addNotify();
054:
055: assertNotNull(choice.getGraphics());
056: Dimension minSize = choice.getMinimumSize();
057: assertTrue(minSize.height > 0);
058: assertTrue(minSize.width > 0);
059: f.dispose();
060: assertNull(choice.getGraphics());
061: }
062:
063: public final void testGetAccessibleContext() {
064: //TODO Implement getAccessibleContext().
065: }
066:
067: public final void testParamString() {
068: String str = choice.paramString();
069: assertEquals("name is correct", 0, str.indexOf("choice"));
070: assertTrue(str.indexOf("current=" + null) > 0);
071: }
072:
073: public final void testProcessEvent() {
074: eventProcessed = false;
075: choice.addKeyListener(new KeyAdapter() {
076: @Override
077: public void keyReleased(KeyEvent ke) {
078: eventProcessed = true;
079: }
080: });
081: choice.processEvent(new KeyEvent(choice, KeyEvent.KEY_RELEASED,
082: 0, 0, 0, 'q'));
083: assertTrue(eventProcessed);
084: }
085:
086: public final void testGetListeners() {
087: assertEquals(0, choice.getListeners(KeyListener.class).length);
088: KeyAdapter listener = new KeyAdapter() {
089: };
090: choice.addKeyListener(listener);
091: Class<KeyListener> clazz = KeyListener.class;
092: assertEquals(1, choice.getListeners(clazz).length);
093: assertEquals(listener, choice.getListeners(clazz)[0]);
094: choice.removeKeyListener(listener);
095: assertEquals(0, choice.getListeners(clazz).length);
096: }
097:
098: public final void testChoice() {
099: assertFalse(choice.isLightweight());
100: assertEquals(-1, choice.getSelectedIndex());
101: assertNull(choice.getSelectedItem());
102: }
103:
104: /*
105: * Class under test for void add(java.lang.String)
106: */
107: public final void testAddString() {
108: String item = "item";
109: choice.add(item);
110: assertSame(item, choice.getItem(0));
111: choice.add(item = "item1");
112: assertSame(item, choice.getItem(1));
113: boolean npe = false;
114: try {
115: choice.add(item = null);
116: } catch (NullPointerException e) {
117: npe = true;
118: }
119: assertTrue(npe);
120: }
121:
122: /*
123: * Class under test for void remove(java.lang.String)
124: */
125: public final void testRemoveString() {
126: String item = "q";
127: boolean exception = false;
128: try {
129: choice.remove(item);
130: } catch (IllegalArgumentException e) {
131: exception = true;
132: }
133: assertTrue(exception);
134: choice.add("pp");
135: choice.add(item);
136: choice.add("qq");
137: choice.add(item);
138: choice.select(1);
139: choice.remove(item);
140: assertEquals(3, choice.getItemCount());
141: assertEquals("pp", choice.getItem(0));
142: assertEquals("qq", choice.getItem(1));
143: assertSame(item, choice.getItem(2));
144: assertEquals(0, choice.getSelectedIndex());
145: }
146:
147: /*
148: * Class under test for void remove(int)
149: */
150: public final void testRemoveint() {
151: int pos = 0;
152: boolean exception = false;
153: try {
154: choice.remove(pos);
155: } catch (IndexOutOfBoundsException e) {
156: exception = true;
157: }
158: assertTrue(exception);
159: String item = "item";
160: choice.add(item);
161: choice.add(item);
162: choice.remove(1);
163: assertEquals(1, choice.getItemCount());
164: assertSame(item, choice.getItem(0));
165: choice.select(0);
166: assertSame(item, choice.getSelectedItem());
167: choice.remove(0);
168: assertEquals(0, choice.getItemCount());
169: assertEquals(-1, choice.getSelectedIndex());
170: assertNull(choice.getSelectedItem());
171: choice.add(item = "item1");
172: choice.add("item2");
173: choice.add("item3");
174: choice.select(2);
175: // test shift of selected index:
176: choice.remove(0);
177: assertEquals(1, choice.getSelectedIndex());
178: choice.remove(1);
179: assertEquals(0, choice.getSelectedIndex());
180: }
181:
182: public final void testRemoveAll() {
183: choice.add("ppp");
184: choice.add("qqq");
185: assertEquals(2, choice.getItemCount());
186: choice.removeAll();
187: assertEquals(0, choice.getItemCount());
188: }
189:
190: public final void testInsert() {
191: String item = "item";
192: boolean iae = false;
193: try {
194: choice.insert(item, -1);
195: } catch (IllegalArgumentException e) {
196: iae = true;
197: }
198: assertTrue(iae);
199: int pos = 0;
200: choice.insert(item = "item1", pos);
201: assertSame(item, choice.getItem(pos));
202: assertEquals(0, choice.getSelectedIndex());
203: choice.add("item2");
204: choice.add("item3");
205: choice.select(2);
206: assertEquals(2, choice.getSelectedIndex());
207: choice.insert(item = "item", pos = 1);
208: assertSame(item, choice.getItem(pos));
209: assertEquals("item2", choice.getItem(pos + 1));
210: assertEquals("item3", choice.getItem(pos + 2));
211: assertEquals(0, choice.getSelectedIndex());
212: choice.select(2);
213: choice.insert(item = "end", choice.getItemCount() + 100);
214: assertSame(item, choice.getItem(choice.getItemCount() - 1));
215: assertEquals(2, choice.getSelectedIndex());
216:
217: // Regression test for HARMONY-2468
218: try {
219: new Choice().insert(null, 0);
220: fail("NullPointerException expected"); //$NON-NLS-1$
221: } catch (NullPointerException ex) {
222: // expected
223: }
224: }
225:
226: public final void testGetSelectedObjects() {
227: assertNull(choice.getSelectedObjects());
228: choice.add("item");
229: String selItem = "item1";
230: choice.add(selItem);
231: choice.add("item2");
232: choice.select(selItem);
233: Object[] objs = choice.getSelectedObjects();
234: assertNotNull(objs);
235: assertEquals(1, objs.length);
236: assertSame(selItem, objs[0]);
237: }
238:
239: public final void testGetItem() {
240: boolean exception = false;
241: try {
242: choice.getItem(0);
243: } catch (IndexOutOfBoundsException ex) {
244: exception = true;
245: }
246: assertTrue(exception);
247: String item = "item";
248: choice.add(item);
249: assertSame(item, choice.getItem(0));
250: choice.add(item = "item1");
251: assertSame(item, choice.getItem(1));
252: }
253:
254: public final void testAddItem() {
255: String item = "item";
256: choice.addItem(item);
257: assertSame(item, choice.getItem(0));
258: choice.addItem(item = "item1");
259: assertSame(item, choice.getItem(1));
260: boolean npe = false;
261: try {
262: choice.addItem(item = null);
263: } catch (NullPointerException e) {
264: npe = true;
265: }
266: assertTrue(npe);
267: }
268:
269: @SuppressWarnings("deprecation")
270: public final void testCountItems() {
271: assertEquals(0, choice.countItems());
272: }
273:
274: public final void testGetItemCount() {
275: assertEquals(0, choice.getItemCount());
276: }
277:
278: public final void testGetSelectedIndex() {
279: assertEquals(-1, choice.getSelectedIndex());
280: choice.add("qqq");
281: assertEquals(0, choice.getSelectedIndex());
282: choice.select(0);
283: assertEquals(0, choice.getSelectedIndex());
284: choice.add("ppp");
285: choice.select(1);
286: assertEquals(1, choice.getSelectedIndex());
287: }
288:
289: public final void testGetSelectedItem() {
290: assertNull(choice.getSelectedItem());
291: }
292:
293: /*
294: * Class under test for void select(int)
295: */
296: public final void testSelectint() {
297: choice.add("item");
298: choice.add("item1");
299: choice.add("item2");
300: int idx = 0;
301: choice.select(idx);
302: assertEquals(idx, choice.getSelectedIndex());
303: choice.select(idx = 1);
304: assertEquals(idx, choice.getSelectedIndex());
305: boolean iae = false;
306: try {
307: choice.select(1000);
308: } catch (IllegalArgumentException e) {
309: iae = true;
310: }
311: assertTrue(iae);
312: iae = false;
313: try {
314: choice.select(-1);
315: } catch (IllegalArgumentException e) {
316: iae = true;
317: }
318: assertTrue(iae);
319: }
320:
321: /*
322: * Class under test for void select(java.lang.String)
323: */
324: public final void testSelectString() {
325: String item = "item";
326: choice.add(item);
327: choice.add("item1");
328: choice.add(item);
329: choice.select("item");
330: assertEquals(item, choice.getSelectedItem());
331: assertEquals(0, choice.getSelectedIndex());
332: choice.select(item = "item1");
333: assertEquals(item, choice.getSelectedItem());
334: choice.select("q");
335: assertEquals(item, choice.getSelectedItem());
336: choice.select(null);
337: assertEquals(item, choice.getSelectedItem());
338:
339: }
340:
341: public final void testAddGetRemoveItemListener() {
342: assertEquals(0, choice.getItemListeners().length);
343:
344: ItemListener listener = new ItemListener() {
345: public void itemStateChanged(ItemEvent ie) {
346: }
347: };
348:
349: choice.addItemListener(listener);
350: assertEquals(1, choice.getItemListeners().length);
351: assertSame(listener, choice.getItemListeners()[0]);
352:
353: choice.removeItemListener(listener);
354: assertEquals(0, choice.getItemListeners().length);
355: }
356:
357: public final void testProcessItemEvent() {
358: eventProcessed = false;
359: choice.addItemListener(new ItemListener() {
360: public void itemStateChanged(ItemEvent ie) {
361: eventProcessed = true;
362: }
363: });
364: choice.processEvent(new ItemEvent(choice,
365: ItemEvent.ITEM_STATE_CHANGED, null,
366: ItemEvent.DESELECTED));
367: assertTrue(eventProcessed);
368: }
369:
370: public void testDeadLoop4887() {
371: final int count[] = new int[1];
372: Component c = new Choice() {
373: public void paint(Graphics g) {
374: count[0]++;
375: if (getItemCount() == 0) {
376: add("item");
377: }
378: select(0);
379: }
380: };
381:
382: Tools.checkDeadLoop(c, count);
383: }
384: }
|