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.KeyAdapter;
023: import java.awt.event.KeyEvent;
024: import java.awt.event.TextEvent;
025: import java.awt.event.TextListener;
026: import java.util.EventListener;
027:
028: import junit.framework.TestCase;
029:
030: public class TextComponentTest extends TestCase {
031:
032: TextComponent textComp;
033: Frame frame;
034: TextListener listener;
035: private boolean eventProcessed;
036:
037: private class MyTextListener implements TextListener {
038:
039: public void textValueChanged(TextEvent te) {
040: eventProcessed = true;
041: }
042:
043: }
044:
045: @Override
046: protected void setUp() throws Exception {
047: super .setUp();
048: textComp = new TextField();
049: listener = new MyTextListener();
050: }
051:
052: @Override
053: protected void tearDown() throws Exception {
054: super .tearDown();
055: if ((frame != null) && frame.isDisplayable()) {
056: frame.dispose();
057: }
058: }
059:
060: /*
061: * Test method for 'java.awt.TextComponent.addNotify()'
062: */
063: public void testAddNotify() {
064: frame = new Frame();
065: frame.add(textComp);
066: assertNull(textComp.getGraphics());
067: assertFalse(textComp.isDisplayable());
068: frame.addNotify();
069: assertTrue(textComp.isDisplayable());
070: assertNotNull(textComp.getGraphics());
071:
072: }
073:
074: /*
075: * Test method for 'java.awt.TextComponent.getAccessibleContext()'
076: */
077: public void testGetAccessibleContext() {
078: assertTrue(textComp.getAccessibleContext() instanceof TextComponent.AccessibleAWTTextComponent);
079: }
080:
081: /*
082: * Test method for 'java.awt.TextComponent.getBackground()'
083: */
084: public void testGetBackground() {
085: assertNull(textComp.getBackground());
086: frame = new Frame();
087: frame.add(textComp);
088: frame.addNotify();
089: Color bkColor = frame.getBackground();
090: assertEquals(bkColor, textComp.getBackground());
091: textComp.setEditable(false);
092: assertSame(bkColor = SystemColor.control, textComp
093: .getBackground());
094: textComp.setEditable(true);
095: assertEquals(bkColor = frame.getBackground(), textComp
096: .getBackground());
097:
098: }
099:
100: /*
101: * Test method for 'java.awt.TextComponent.paramString()'
102: */
103: public void testParamString() {
104: String paramStr = textComp.paramString();
105: assertTrue(paramStr.indexOf(",text=") >= 0);
106: assertTrue(paramStr.indexOf(",editable") >= 0);
107: assertTrue(paramStr.indexOf(",selection=0-0") >= 0);
108:
109: }
110:
111: /*
112: * Test method for 'java.awt.TextComponent.removeNotify()'
113: */
114: public void testRemoveNotify() {
115: frame = new Frame();
116: frame.add(textComp);
117: frame.addNotify();
118: assertNotNull(textComp.getGraphics());
119: textComp.removeNotify();
120: assertFalse(textComp.isDisplayable());
121: assertNull(textComp.getGraphics());
122: }
123:
124: /*
125: * Test method for 'java.awt.TextComponent.setBackground(Color)'
126: */
127: public void testSetBackground() {
128: Color bkColor = Color.GREEN;
129: textComp.setBackground(bkColor);
130: assertSame(bkColor, textComp.getBackground());
131: frame = new Frame();
132: frame.add(textComp);
133: frame.addNotify();
134: textComp.setBackground(null);
135: bkColor = frame.getBackground();
136: assertEquals(bkColor, textComp.getBackground());
137: }
138:
139: /*
140: * Test method for 'java.awt.TextComponent.processEvent(AWTEvent)'
141: */
142: public void testProcessEvent() {
143: eventProcessed = false;
144: textComp.addKeyListener(new KeyAdapter() {
145: @Override
146: public void keyPressed(KeyEvent a0) {
147: eventProcessed = true;
148: }
149: });
150: textComp.processEvent(new KeyEvent(textComp,
151: KeyEvent.KEY_PRESSED, 0, 0, 0, 's'));
152: assertTrue(eventProcessed);
153: }
154:
155: /*
156: * Test method for 'java.awt.TextComponent.getListeners(Class)'
157: */
158: public void testGetListeners() {
159: Class<TextListener> cls = TextListener.class;
160: EventListener[] listeners = textComp.getListeners(cls);
161: assertEquals(0, listeners.length);
162: textComp.addTextListener(listener);
163: assertEquals(1, (listeners = textComp.getListeners(cls)).length);
164: assertSame(listener, listeners[0]);
165:
166: }
167:
168: /*
169: * Test method for 'java.awt.TextComponent.enableInputMethods(boolean)'
170: */
171: public void testEnableInputMethods() {
172: // TODO: write test when implemented
173:
174: }
175:
176: /*
177: * Test method for 'java.awt.TextComponent.TextComponent()'
178: */
179: public void testTextComponent() {
180: assertNotNull(textComp);
181: assertNull(textComp.textListener);
182: }
183:
184: /*
185: * Test method for 'java.awt.TextComponent.getText()'
186: */
187: public void testGetText() {
188: assertEquals("text is an empty string by default", "", textComp
189: .getText());
190: }
191:
192: /*
193: * Test method for 'java.awt.TextComponent.getCaretPosition()'
194: */
195: public void testGetCaretPosition() {
196: String text = "txt";
197: assertEquals(0, textComp.getCaretPosition());
198: textComp.setText(text);
199: assertEquals(0, textComp.getCaretPosition());
200: frame = new Frame();
201: frame.add(textComp);
202: assertEquals(0, textComp.getCaretPosition());
203: frame.setSize(100, 100);
204: frame.setVisible(true);
205:
206: assertEquals("caret position not updated by setText()", 0,
207: textComp.getCaretPosition());
208: }
209:
210: /*
211: * Test method for 'java.awt.TextComponent.getSelectedText()'
212: */
213: public void testGetSelectedText() {
214: assertEquals("empty string is selected", "", textComp
215: .getSelectedText());
216: }
217:
218: /*
219: * Test method for 'java.awt.TextComponent.getSelectionEnd()'
220: */
221: public void testGetSelectionEnd() {
222: assertEquals("default selection end is 0", 0, textComp
223: .getSelectionEnd());
224: }
225:
226: /*
227: * Test method for 'java.awt.TextComponent.getSelectionStart()'
228: */
229: public void testGetSelectionStart() {
230: assertEquals("default selection start is 0", 0, textComp
231: .getSelectionStart());
232: }
233:
234: /*
235: * Test method for 'java.awt.TextComponent.isEditable()'
236: */
237: public void testIsEditable() {
238: assertTrue("editable by default", textComp.isEditable());
239:
240: }
241:
242: /*
243: * Test method for 'java.awt.TextComponent.select(int, int)'
244: */
245: public void testSelect() {
246: textComp.setText("First line of text.");
247: int start = 6;
248: int end = 10;
249: textComp.select(start, end); // select
250: assertEquals(start, textComp.getSelectionStart());
251: assertEquals(end, textComp.getSelectionEnd());
252: assertEquals("line", textComp.getSelectedText());
253: textComp.select(start = 13, end = start); // deselect
254: assertEquals(start, textComp.getSelectionStart());
255: assertEquals(end, textComp.getSelectionEnd());
256: assertEquals("", textComp.getSelectedText());
257:
258: }
259:
260: /*
261: * Test method for 'java.awt.TextComponent.selectAll()'
262: */
263: public void testSelectAll() {
264: String text = "Some text";
265: textComp.setText(text);
266: assertEquals("", textComp.getSelectedText());
267: textComp.selectAll();
268: assertEquals(0, textComp.getSelectionStart());
269: assertEquals(text.length(), textComp.getSelectionEnd());
270: assertEquals(text, textComp.getSelectedText());
271:
272: }
273:
274: /*
275: * Test method for 'java.awt.TextComponent.setCaretPosition(int)'
276: */
277: public void testSetCaretPosition() {
278: int pos = 5;
279: textComp.setCaretPosition(pos);
280: assertEquals(0, textComp.getCaretPosition());
281: textComp.setText("Some text");
282: textComp.setCaretPosition(pos);
283: assertEquals(pos, textComp.getCaretPosition());
284: frame = new Frame();
285: frame.add(textComp);
286: textComp.setText("new");
287: assertEquals(pos, textComp.getCaretPosition());
288: frame.addNotify();
289: assertEquals("making components displayable corrects"
290: + " invalid caret position", textComp.getText()
291: .length(), textComp.getCaretPosition());
292: textComp.setText("new text");
293: assertEquals(
294: "setText() on displayable component resets caret position",
295: 0, textComp.getCaretPosition());
296: textComp.setCaretPosition(pos = 8);
297: try {
298: Thread.sleep(400);
299: } catch (InterruptedException e) {
300: e.printStackTrace();
301: }
302: assertEquals(textComp.getText().length(), textComp
303: .getCaretPosition());
304: try {
305: textComp.setCaretPosition(pos = -1);
306: } catch (IllegalArgumentException iae) {
307: assertEquals("caret is at the end of document", textComp
308: .getText().length(), textComp.getCaretPosition());
309: return;
310: }
311: fail("No exception was thrown!");
312:
313: }
314:
315: /*
316: * Test method for 'java.awt.TextComponent.setEditable(boolean)'
317: */
318: public void testSetEditable() {
319: textComp.setEditable(false);
320: assertFalse(textComp.isEditable());
321: textComp.setEditable(true);
322: assertTrue(textComp.isEditable());
323:
324: }
325:
326: /*
327: * Test method for 'java.awt.TextComponent.setSelectionEnd(int)'
328: */
329: public void testSetSelectionEnd() {
330: textComp.setText("This is some text.");
331: int end = 4;
332: textComp.setSelectionEnd(end);
333: assertEquals(end, textComp.getSelectionEnd());
334: assertEquals("This", textComp.getSelectedText());
335: textComp.setSelectionStart(5);
336: end = textComp.getSelectionStart() - 1;
337: textComp.setSelectionEnd(end);
338: assertEquals(textComp.getSelectionStart(), textComp
339: .getSelectionEnd());
340: textComp.setSelectionEnd(end = 1000);
341: assertEquals(textComp.getText().length(), textComp
342: .getSelectionEnd());
343:
344: }
345:
346: /*
347: * Test method for 'java.awt.TextComponent.setSelectionStart(int)'
348: */
349: public void testSetSelectionStart() {
350: textComp.setText("This is some text.");
351: int start = 13;
352: textComp.setSelectionStart(start);
353: textComp.setSelectionEnd(start + 4);
354: assertEquals(start, textComp.getSelectionStart());
355: assertEquals("text", textComp.getSelectedText());
356: textComp.setSelectionStart(textComp.getSelectionEnd() + 1);
357: assertEquals(textComp.getSelectionEnd(), textComp
358: .getSelectionStart());
359: textComp.setSelectionStart(start = -1);
360: assertEquals(0, textComp.getSelectionStart());
361: }
362:
363: /*
364: * Test method for 'java.awt.TextComponent.setText(String)'
365: */
366: public void testSetText() {
367: String text = "Some text";
368: textComp.setText(text);
369: assertEquals(text, textComp.getText());
370: textComp.setText(null);
371: assertEquals("", textComp.getText());
372: }
373:
374: /*
375: * Test method for 'java.awt.TextComponent.addTextListener(TextListener)'
376: */
377: public void testAddRemoveTextListener() {
378: textComp.addTextListener(listener);
379: assertSame(listener, textComp.textListener);
380: TextListener newListener = new MyTextListener();
381: textComp.addTextListener(newListener);
382: assertTrue(
383: "if there are several listeners multicaster is used",
384: textComp.textListener instanceof AWTEventMulticaster);
385: AWTEventMulticaster aem = (AWTEventMulticaster) textComp.textListener;
386: assertSame(listener, aem.a);
387: textComp.removeTextListener(listener);
388: assertTrue("if there is only one listener then it is used",
389: textComp.textListener instanceof MyTextListener);
390: assertSame(newListener, textComp.textListener);
391: textComp.removeTextListener(newListener);
392: assertNull(textComp.textListener);
393:
394: }
395:
396: /*
397: * Test method for 'java.awt.TextComponent.getTextListeners()'
398: */
399: public void testGetTextListeners() {
400: TextListener[] listeners = textComp.getTextListeners();
401: assertEquals(0, listeners.length);
402: textComp.addTextListener(listener);
403: assertEquals(1,
404: (listeners = textComp.getTextListeners()).length);
405: assertSame(listener, listeners[0]);
406: }
407:
408: /*
409: * Test method for 'java.awt.TextComponent.processTextEvent(TextEvent)'
410: */
411: public void testProcessTextEvent() {
412: eventProcessed = false;
413: textComp.addTextListener(listener);
414: textComp.processEvent(new TextEvent(textComp,
415: TextEvent.TEXT_VALUE_CHANGED));
416: assertTrue("text event processed", eventProcessed);
417: }
418:
419: public void testDeadLoop4887() {
420: final int count[] = new int[1];
421: Component c = new TextArea() {
422: public void paint(Graphics g) {
423: count[0]++;
424: setEditable(true);
425: setEnabled(true);
426: }
427: };
428:
429: Tools.checkDeadLoop(c, count);
430: }
431: }
|