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.ActionEvent;
023: import java.awt.event.ActionListener;
024: import java.awt.event.KeyAdapter;
025: import java.awt.event.KeyEvent;
026: import java.util.EventListener;
027:
028: import junit.framework.TestCase;
029:
030: public class TextFieldTest extends TestCase {
031: TextField tf;
032: Frame frame;
033: private boolean eventProcessed;
034: ActionListener listener;
035:
036: private class MyActionListener implements ActionListener {
037: public void actionPerformed(ActionEvent ae) {
038: eventProcessed = true;
039: }
040: }
041:
042: @Override
043: protected void setUp() throws Exception {
044: super .setUp();
045: tf = new TextField();
046: listener = new MyActionListener();
047: }
048:
049: @Override
050: protected void tearDown() throws Exception {
051: super .tearDown();
052: if ((frame != null) && frame.isDisplayable()) {
053: frame.dispose();
054: }
055: }
056:
057: /*
058: * Test method for 'java.awt.TextField.addNotify()'
059: */
060: public void testAddNotify() {
061: frame = new Frame();
062: frame.add(tf);
063: assertNull(tf.getGraphics());
064: assertFalse(tf.isDisplayable());
065: frame.addNotify();
066: assertTrue(tf.isDisplayable());
067: assertNotNull(tf.getGraphics());
068: }
069:
070: /*
071: * Test method for 'java.awt.TextField.getAccessibleContext()'
072: */
073: public void testGetAccessibleContext() {
074: assertTrue(tf.getAccessibleContext() instanceof TextField.AccessibleAWTTextField);
075: }
076:
077: /*
078: * Test method for 'java.awt.TextField.paramString()'
079: */
080: public void testParamString() {
081: tf.setEchoChar('q');
082: String paramStr = tf.paramString();
083: assertEquals(0, paramStr.indexOf("textfield"));
084: assertTrue(paramStr.indexOf(",echo=q") >= 0);
085: }
086:
087: /*
088: * Test method for 'java.awt.TextField.getMinimumSize()'
089: */
090: public void testGetMinimumSize() {
091: Dimension minSize = new Dimension();
092: assertEquals(minSize, tf.getMinimumSize());
093: minSize.setSize(130, 160);
094: tf.setMinimumSize(minSize);
095: assertNotSame(minSize, tf.getMinimumSize());
096: assertEquals(minSize, tf.getMinimumSize());
097: tf.setMinimumSize(null);
098: assertEquals(new Dimension(), tf.getMinimumSize());
099: makeDisplayable();
100: assertEquals("By default minimum size is set for 0 columns", tf
101: .getMinimumSize(0), tf.getMinimumSize());
102: tf.setText("__");
103: assertEquals("for 2 chars minimum size is 2 columns", tf
104: .getMinimumSize(2), tf.getMinimumSize());
105: }
106:
107: /*
108: * Test method for 'java.awt.TextField.minimumSize()'
109: */
110: @SuppressWarnings("deprecation")
111: public void testMinimumSize() {
112: Dimension minSize = new Dimension();
113: assertEquals(minSize, tf.minimumSize());
114: minSize.setSize(60, 10);
115: tf.setMinimumSize(minSize);
116: assertNotSame(minSize, tf.minimumSize());
117: assertEquals(minSize, tf.minimumSize());
118: tf.setMinimumSize(null);
119: assertEquals(new Dimension(), tf.minimumSize());
120: makeDisplayable();
121: assertEquals("By default minimum size is set for 0 columns", tf
122: .minimumSize(0), tf.minimumSize());
123:
124: int cols = 80;
125: tf.setColumns(cols);
126: assertEquals(tf.minimumSize(cols), tf.minimumSize());
127: }
128:
129: /*
130: * Test method for 'java.awt.TextField.getPreferredSize()'
131: */
132: public void testGetPreferredSize() {
133: Dimension prefSize = new Dimension();
134: assertEquals(prefSize, tf.getPreferredSize());
135: prefSize.setSize(5, 6);
136: tf.setPreferredSize(prefSize);
137: assertNotSame(prefSize, tf.getPreferredSize());
138: assertEquals(prefSize, tf.getPreferredSize());
139: tf.setPreferredSize(null);
140: assertEquals(new Dimension(), tf.getPreferredSize());
141: makeDisplayable();
142: assertEquals(
143: "By default preferred size is equal to minimum size",
144: tf.getMinimumSize(), tf.getPreferredSize());
145: }
146:
147: /*
148: * Test method for 'java.awt.TextField.preferredSize()'
149: */
150: @SuppressWarnings("deprecation")
151: public void testPreferredSize() {
152: Dimension prefSize = new Dimension();
153: assertEquals(tf.minimumSize(), tf.preferredSize());
154: prefSize.setSize(80, 20);
155: tf.setPreferredSize(prefSize);
156: assertNotSame(prefSize, tf.preferredSize());
157: assertEquals(prefSize, tf.preferredSize());
158: tf.setPreferredSize(null);
159: assertEquals(new Dimension(), tf.preferredSize());
160: makeDisplayable();
161: assertEquals(
162: "By default preferred size is equal to minimum size",
163: tf.minimumSize(), tf.preferredSize());
164: }
165:
166: /*
167: * Test method for 'java.awt.TextField.processEvent(AWTEvent)'
168: */
169: public void testProcessEvent() {
170: eventProcessed = false;
171: tf.addKeyListener(new KeyAdapter() {
172: @Override
173: public void keyTyped(KeyEvent ke) {
174: eventProcessed = true;
175: }
176: });
177: tf.processEvent(new KeyEvent(tf, KeyEvent.KEY_TYPED, 0, 0, 0,
178: 's'));
179: assertTrue(eventProcessed);
180: }
181:
182: /*
183: * Test method for 'java.awt.TextField.getListeners(Class)'
184: */
185: public void testGetListeners() {
186: Class<ActionListener> cls = ActionListener.class;
187: EventListener[] listeners = tf.getListeners(cls);
188: assertEquals(0, listeners.length);
189: tf.addActionListener(listener);
190: assertEquals(1, (listeners = tf.getListeners(cls)).length);
191: assertSame(listener, listeners[0]);
192: }
193:
194: /*
195: * Test method for 'java.awt.TextField.setText(String)'
196: */
197: public void testSetText() {
198: String text = "Some text";
199: tf.setText(text);
200: assertEquals(text, tf.getText());
201: text += "\nsecond line";
202: tf.setText(text);
203: // multi-line text is allowed:
204: assertEquals(text, tf.getText());
205: tf.setText(null);
206: assertEquals("", tf.getText());
207: tf.setText(text = "some old text");
208: int pos = 8;
209: tf.setCaretPosition(pos);
210: // assertEquals(pos, tf.getCaretPosition());
211: tf.setText(text = "new");
212: assertEquals(text, tf.getText());
213: // caret position remains invalid:
214: assertEquals(pos, tf.getCaretPosition());
215: makeDisplayable();
216: frame.setSize(100, 100);
217: frame.setVisible(true);
218:
219: // on a displayable component caret position
220: // is always correct:
221: assertEquals(text.length(), tf.getCaretPosition());
222: }
223:
224: /*
225: * Test method for 'java.awt.TextField.TextField(String)'
226: */
227: public void testTextFieldString() {
228: String text = "text";
229: tf = new TextField(text);
230: assertEquals(text, tf.getText());
231: assertEquals(text.length(), tf.getColumns());
232:
233: tf = new TextField(text = null);
234: assertEquals("", tf.getText());
235: assertEquals(0, tf.getColumns());
236: }
237:
238: /*
239: * Test method for 'java.awt.TextField.TextField(int)'
240: */
241: public void testTextFieldInt() {
242: int cols = 80;
243: tf = new TextField(cols);
244: assertEquals("", tf.getText());
245: assertEquals(cols, tf.getColumns());
246: tf = new TextField(cols = -1);
247: assertEquals("", tf.getText());
248: assertEquals(0, tf.getColumns());
249: }
250:
251: /*
252: * Test method for 'java.awt.TextField.TextField()'
253: */
254: public void testTextField() {
255: assertNotNull(tf);
256: assertEquals("", tf.getText());
257: assertEquals(0, tf.getColumns());
258: }
259:
260: /*
261: * Test method for 'java.awt.TextField.TextField(String, int)'
262: */
263: public void testTextFieldStringInt() {
264: int cols = 2;
265: String text = "text";
266: tf = new TextField(text, cols);
267: assertEquals(text, tf.getText());
268: assertEquals(cols, tf.getColumns());
269: }
270:
271: /*
272: * Test method for 'java.awt.TextField.getColumns()'
273: */
274: public void testGetColumns() {
275: assertEquals(0, tf.getColumns());
276: tf.setText("some text");
277: assertEquals(0, tf.getColumns());
278:
279: }
280:
281: /*
282: * Test method for 'java.awt.TextField.getMinimumSize(int)'
283: */
284: public void testGetMinimumSizeInt() {
285: int cols = 1;
286: Dimension minSize = new Dimension();
287: assertEquals(minSize, tf.getMinimumSize(cols));
288: minSize.setSize(120, 130);
289: tf.setMinimumSize(minSize);
290: assertEquals(minSize, tf.getMinimumSize(cols));
291: tf.setMinimumSize(null);
292: assertEquals(new Dimension(), tf.getMinimumSize(cols));
293: makeDisplayable();
294: minSize.setSize(tf.getMinimumSize(cols));
295: int w = minSize.width;
296: int h = minSize.height;
297: assertTrue(w > 0);
298: assertTrue(tf.getMinimumSize(0).width > 0);
299: assertTrue(tf.getMinimumSize(cols).height > 0);
300: assertEquals(tf.getMinimumSize(0).height, h);
301: int dw = (tf.getMinimumSize(cols * 2).width - w);
302: int dw1 = (tf.getMinimumSize(cols * 3).width - tf
303: .getMinimumSize(cols * 2).width);
304: assertEquals(dw, dw1);
305:
306: }
307:
308: /*
309: * Test method for 'java.awt.TextField.getPreferredSize(int)'
310: */
311: public void testGetPreferredSizeInt() {
312: int cols = 3;
313: Dimension prefSize = new Dimension();
314: assertEquals(tf.getMinimumSize(cols), tf.getPreferredSize(cols));
315: prefSize.setSize(120, 13);
316: tf.setPreferredSize(prefSize);
317: assertEquals(prefSize, tf.getPreferredSize(cols));
318: tf.setPreferredSize(null);
319: assertEquals(new Dimension(), tf.getPreferredSize(cols));
320: makeDisplayable();
321: assertEquals(tf.getMinimumSize(cols), tf.getPreferredSize(cols));
322: tf.setPreferredSize(prefSize);
323: assertEquals(tf.getMinimumSize(cols), tf.getPreferredSize(cols));
324: }
325:
326: /*
327: * Test method for 'java.awt.TextField.minimumSize(int)'
328: */
329: @SuppressWarnings("deprecation")
330: public void testMinimumSizeInt() {
331: makeDisplayable();
332: int cols = 25;
333: assertEquals(tf.getMinimumSize(cols), tf.minimumSize(cols));
334: }
335:
336: private void makeDisplayable() {
337: frame = new Frame();
338: frame.add(tf);
339: frame.addNotify();
340: }
341:
342: /*
343: * Test method for 'java.awt.TextField.preferredSize(int)'
344: */
345: @SuppressWarnings("deprecation")
346: public void testPreferredSizeInt() {
347: makeDisplayable();
348: int cols = 125;
349: assertEquals(tf.getPreferredSize(cols), tf.preferredSize(cols));
350: }
351:
352: /*
353: * Test method for 'java.awt.TextField.setColumns(int)'
354: */
355: public void testSetColumns() {
356: int cols = 80;
357: tf.setColumns(cols);
358: assertEquals(cols, tf.getColumns());
359: try {
360: tf.setColumns(-1);
361: } catch (IllegalArgumentException iae) {
362: assertEquals(cols, tf.getColumns());
363: return;
364: }
365: fail("no exception was thrown!");
366: }
367:
368: /*
369: * Test method for 'java.awt.TextField.echoCharIsSet()'
370: */
371: public void testEchoCharIsSet() {
372: assertFalse(tf.echoCharIsSet());
373:
374: }
375:
376: /*
377: * Test method for 'java.awt.TextField.getEchoChar()'
378: */
379: public void testGetEchoChar() {
380: assertEquals('\0', tf.getEchoChar());
381:
382: }
383:
384: /*
385: * Test method for 'java.awt.TextField.setEchoChar(char)'
386: */
387: public void testSetEchoChar() {
388: char echoChar = 'q';
389: tf.setEchoChar(echoChar);
390: assertTrue(tf.echoCharIsSet());
391: assertEquals(echoChar, tf.getEchoChar());
392: tf.setEchoChar(echoChar = '\0');
393: assertFalse(tf.echoCharIsSet());
394: assertEquals(echoChar, tf.getEchoChar());
395:
396: }
397:
398: /*
399: * Test method for 'java.awt.TextField.setEchoCharacter(char)'
400: */
401: @SuppressWarnings("deprecation")
402: public void testSetEchoCharacter() {
403: char echoChar = '*';
404: tf.setEchoCharacter(echoChar);
405: assertTrue(tf.echoCharIsSet());
406: assertEquals(echoChar, tf.getEchoChar());
407: }
408:
409: /*
410: * Test method for 'java.awt.TextField.addActionListener(ActionListener)'
411: */
412: public void testAddGetRemoveActionListener() {
413: assertTrue(tf.getActionListeners().length == 0);
414:
415: tf.addActionListener(listener);
416: assertTrue(tf.getActionListeners().length == 1);
417: assertTrue(tf.getActionListeners()[0] == listener);
418:
419: tf.removeActionListener(listener);
420: assertTrue(tf.getActionListeners().length == 0);
421: }
422:
423: /*
424: * Test method for 'java.awt.TextField.processActionEvent(ActionEvent)'
425: */
426: public void testProcessActionEvent() {
427: eventProcessed = false;
428: tf.addActionListener(listener);
429: tf.processEvent(new ActionEvent(tf,
430: ActionEvent.ACTION_PERFORMED, null, 0, 0));
431: assertTrue(eventProcessed);
432: }
433:
434: public void testDeadLoop4887() {
435: final int count[] = new int[1];
436: Component c = new TextField() {
437: public void paint(Graphics g) {
438: count[0]++;
439: setEchoChar(' ');
440: }
441: };
442:
443: Tools.checkDeadLoop(c, count);
444: }
445:
446: }
|