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 junit.framework.TestCase;
023:
024: public class TextAreaTest extends TestCase {
025: TextArea area;
026: Frame frame;
027:
028: @Override
029: protected void setUp() throws Exception {
030: super .setUp();
031: area = new TextArea();
032: }
033:
034: @Override
035: protected void tearDown() throws Exception {
036: super .tearDown();
037: if ((frame != null) && frame.isDisplayable()) {
038: frame.dispose();
039: }
040: }
041:
042: /*
043: * Test method for 'java.awt.TextArea.addNotify()'
044: */
045: public void testAddNotify() {
046: frame = new Frame();
047: frame.add(area);
048: assertNull(area.getGraphics());
049: assertFalse(area.isDisplayable());
050: frame.addNotify();
051: assertTrue(area.isDisplayable());
052: assertNotNull(area.getGraphics());
053: }
054:
055: /*
056: * Test method for 'java.awt.TextArea.getAccessibleContext()'
057: */
058: public void testGetAccessibleContext() {
059: assertTrue(area.getAccessibleContext() instanceof TextArea.AccessibleAWTTextArea);
060:
061: }
062:
063: /*
064: * Test method for 'java.awt.TextArea.paramString()'
065: */
066: public void testParamString() {
067: String paramStr = area.paramString();
068: assertEquals(0, paramStr.indexOf("text"));
069: assertTrue(paramStr.indexOf(",rows=0") >= 0);
070: assertTrue(paramStr.indexOf(",columns=0") >= 0);
071: assertTrue(paramStr.indexOf(",scrollbarVisibility=both") >= 0);
072: }
073:
074: /*
075: * Test method for 'java.awt.TextArea.getMinimumSize()'
076: */
077: public void testGetMinimumSize() {
078: Dimension minSize = new Dimension();
079: assertEquals(minSize, area.getMinimumSize());
080: minSize.setSize(13, 16);
081: area.setMinimumSize(minSize);
082: assertNotSame(minSize, area.getMinimumSize());
083: assertEquals(minSize, area.getMinimumSize());
084: area.setMinimumSize(null);
085: assertEquals(new Dimension(), area.getMinimumSize());
086: frame = new Frame();
087: frame.add(area);
088: frame.addNotify();
089: assertEquals(
090: "By default minimum size is set for 10 rows and 60 columns",
091: area.getMinimumSize(10, 60), area.getMinimumSize());
092:
093: }
094:
095: /*
096: * Test method for 'java.awt.TextArea.minimumSize()'
097: */
098: @SuppressWarnings("deprecation")
099: public void testMinimumSize() {
100: Dimension minSize = new Dimension();
101: assertEquals(minSize, area.minimumSize());
102: minSize.setSize(130, 160);
103: area.setMinimumSize(minSize);
104: assertNotSame(minSize, area.minimumSize());
105: assertEquals(minSize, area.minimumSize());
106: area.setMinimumSize(null);
107: assertEquals(new Dimension(), area.minimumSize());
108: frame = new Frame();
109: frame.add(area);
110: frame.addNotify();
111: assertEquals(
112: "By default minimum size is set for 10 rows and 60 columns",
113: area.minimumSize(10, 60), area.minimumSize());
114: int rows = 15;
115: area.setRows(rows);
116: assertEquals(
117: "By default minimum size is set for 10 rows and 60 columns",
118: area.minimumSize(10, 60), area.minimumSize());
119: int cols = 80;
120: area.setColumns(cols);
121: assertEquals(area.minimumSize(rows, cols), area.minimumSize());
122:
123: }
124:
125: /*
126: * Test method for 'java.awt.TextArea.getPreferredSize()'
127: */
128: public void testGetPreferredSize() {
129: Dimension prefSize = new Dimension();
130: assertEquals(prefSize, area.getPreferredSize());
131: prefSize.setSize(4, 5);
132: area.setPreferredSize(prefSize);
133: assertNotSame(prefSize, area.getPreferredSize());
134: assertEquals(prefSize, area.getPreferredSize());
135: area.setPreferredSize(null);
136: assertEquals(new Dimension(), area.getPreferredSize());
137: frame = new Frame();
138: frame.add(area);
139: frame.addNotify();
140: assertEquals(
141: "By default preferred size is equal to minimum size",
142: area.getMinimumSize(), area.getPreferredSize());
143: }
144:
145: /*
146: * Test method for 'java.awt.TextArea.preferredSize()'
147: */
148: @SuppressWarnings("deprecation")
149: public void testPreferredSize() {
150: Dimension prefSize = new Dimension();
151: assertEquals(area.minimumSize(), area.preferredSize());
152: prefSize.setSize(40, 50);
153: area.setPreferredSize(prefSize);
154: assertNotSame(prefSize, area.preferredSize());
155: assertEquals(prefSize, area.preferredSize());
156: area.setPreferredSize(null);
157: assertEquals(new Dimension(), area.preferredSize());
158: frame = new Frame();
159: frame.add(area);
160: frame.addNotify();
161: assertEquals(
162: "By default preferred size is equal to minimum size",
163: area.minimumSize(), area.preferredSize());
164: }
165:
166: /*
167: * Test method for 'java.awt.TextArea.TextArea()'
168: */
169: public void testTextArea() {
170: assertNotNull(area);
171: assertEquals("", area.getText());
172: assertEquals(0, area.getColumns());
173: assertEquals(0, area.getRows());
174: assertEquals(TextArea.SCROLLBARS_BOTH, area
175: .getScrollbarVisibility());
176:
177: }
178:
179: /*
180: * Test method for 'java.awt.TextArea.TextArea(String, int, int, int)'
181: */
182: public void testTextAreaStringIntIntInt() {
183: String text = "text";
184: int cols = 12;
185: int rows = 5;
186: int scrollbars = TextArea.SCROLLBARS_NONE;
187: area = new TextArea(text, rows, cols, scrollbars);
188: assertEquals(text, area.getText());
189: assertEquals(cols, area.getColumns());
190: assertEquals(rows, area.getRows());
191: assertEquals(scrollbars, area.getScrollbarVisibility());
192: scrollbars = -1;
193: area = new TextArea(text, rows, cols, scrollbars);
194: assertEquals(TextArea.SCROLLBARS_BOTH, area
195: .getScrollbarVisibility());
196:
197: }
198:
199: /*
200: * Test method for 'java.awt.TextArea.TextArea(String, int, int)'
201: */
202: public void testTextAreaStringIntInt() {
203: String text = "text";
204: int cols = 12;
205: int rows = 5;
206: area = new TextArea(text, rows, cols);
207: assertEquals(text, area.getText());
208: assertEquals(cols, area.getColumns());
209: assertEquals(rows, area.getRows());
210: assertEquals(TextArea.SCROLLBARS_BOTH, area
211: .getScrollbarVisibility());
212: cols = -12;
213: area = new TextArea(text, rows, cols);
214: assertEquals(0, area.getColumns());
215: assertEquals(rows, area.getRows());
216: rows = -666;
217: area = new TextArea(text, rows, cols);
218: assertEquals(0, area.getColumns());
219: assertEquals(0, area.getColumns());
220: }
221:
222: /*
223: * Test method for 'java.awt.TextArea.TextArea(String)'
224: */
225: public void testTextAreaString() {
226: String text = "text";
227: area = new TextArea(text);
228: assertEquals(text, area.getText());
229: assertEquals(0, area.getColumns());
230: assertEquals(0, area.getRows());
231: assertEquals(TextArea.SCROLLBARS_BOTH, area
232: .getScrollbarVisibility());
233: area = new TextArea(text = null);
234: assertEquals("", area.getText());
235: }
236:
237: /*
238: * Test method for 'java.awt.TextArea.TextArea(int, int)'
239: */
240: public void testTextAreaIntInt() {
241: int cols = 12;
242: int rows = 15;
243: area = new TextArea(rows, cols);
244: assertEquals("", area.getText());
245: assertEquals(cols, area.getColumns());
246: assertEquals(rows, area.getRows());
247: }
248:
249: /*
250: * Test method for 'java.awt.TextArea.append(String)'
251: */
252: public void testAppend() {
253: String text = "text";
254: area.setText(text);
255: String str = "\nappended text";
256:
257: area.append(str);
258: assertEquals(text + str, area.getText());
259: assertEquals(0, area.getRows());
260: assertEquals(0, area.getCaretPosition());
261: }
262:
263: /*
264: * Test method for 'java.awt.TextArea.insert(String, int)'
265: */
266: public void testInsert() {
267: String text = "text";
268: area.setText(text);
269: String str = "inserted text\n";
270: area.insert(str, 0);
271: assertEquals(str + text, area.getText());
272: assertEquals(0, area.getRows());
273: assertEquals(0, area.getCaretPosition());
274: }
275:
276: /*
277: * Test method for 'java.awt.TextArea.appendText(String)'
278: */
279: @SuppressWarnings("deprecation")
280: public void testAppendText() {
281: String text = "text";
282: area.setText(text);
283: String str = "\nappended text";
284: area.appendText(str);
285: assertEquals(text + str, area.getText());
286: assertEquals(0, area.getColumns());
287: }
288:
289: /*
290: * Test method for 'java.awt.TextArea.getColumns()'
291: */
292: public void testGetColumns() {
293: assertEquals(0, area.getColumns());
294: }
295:
296: /*
297: * Test method for 'java.awt.TextArea.getMinimumSize(int, int)'
298: */
299: public void testGetMinimumSizeIntInt() {
300: int rows = 1;
301: int cols = 1;
302: Dimension minSize = new Dimension();
303: assertEquals(minSize, area.getMinimumSize(rows, cols));
304: minSize.setSize(12, 13);
305: area.setMinimumSize(minSize);
306: assertEquals(minSize, area.getMinimumSize(rows, cols));
307: area.setMinimumSize(null);
308: assertEquals(new Dimension(), area.getMinimumSize(rows, cols));
309: frame = new Frame();
310: frame.add(area);
311: frame.addNotify();
312:
313: assertTrue(area.getMinimumSize(rows, cols).width > 0);
314: assertTrue(area.getMinimumSize(rows, cols).height > 0);
315: int dw = (area.getMinimumSize(rows, cols * 2).width - area
316: .getMinimumSize(rows, cols).width);
317: int dw1 = (area.getMinimumSize(rows, cols * 3).width - area
318: .getMinimumSize(rows, cols * 2).width);
319: assertEquals(dw, dw1);
320: int dh = (area.getMinimumSize(rows * 2, cols).height - area
321: .getMinimumSize(rows, cols).height);
322: int dh1 = (area.getMinimumSize(rows * 3, cols).height - area
323: .getMinimumSize(rows * 2, cols).height);
324: assertEquals(dh1, dh);
325:
326: }
327:
328: /*
329: * Test method for 'java.awt.TextArea.getPreferredSize(int, int)'
330: */
331: public void testGetPreferredSizeIntInt() {
332: int rows = 2;
333: int cols = 3;
334: Dimension prefSize = new Dimension();
335: assertEquals(area.getMinimumSize(rows, cols), area
336: .getPreferredSize(rows, cols));
337: prefSize.setSize(12, 13);
338: area.setPreferredSize(prefSize);
339: assertEquals(prefSize, area.getPreferredSize(rows, cols));
340: area.setPreferredSize(null);
341: assertEquals(new Dimension(), area.getPreferredSize(rows, cols));
342: frame = new Frame();
343: frame.add(area);
344: frame.addNotify();
345:
346: assertEquals(area.getMinimumSize(rows, cols), area
347: .getPreferredSize(rows, cols));
348: }
349:
350: /*
351: * Test method for 'java.awt.TextArea.getRows()'
352: */
353: public void testGetRows() {
354: assertEquals(0, area.getRows());
355: }
356:
357: /*
358: * Test method for 'java.awt.TextArea.getScrollbarVisibility()'
359: */
360: public void testGetScrollbarVisibility() {
361: assertEquals(TextArea.SCROLLBARS_BOTH, area
362: .getScrollbarVisibility());
363: }
364:
365: /*
366: * Test method for 'java.awt.TextArea.insertText(String, int)'
367: */
368: @SuppressWarnings("deprecation")
369: public void testInsertText() {
370: String text = "text";
371: area.setText(text);
372: String str = "inserted text\n";
373: area.insertText(str, 0);
374: assertEquals(str + text, area.getText());
375: assertEquals(0, area.getColumns());
376: assertEquals(0, area.getCaretPosition());
377: }
378:
379: /*
380: * Test method for 'java.awt.TextArea.minimumSize(int, int)'
381: */
382: @SuppressWarnings("deprecation")
383: public void testMinimumSizeIntInt() {
384: frame = new Frame();
385: frame.add(area);
386: frame.addNotify();
387: int rows = 10;
388: int cols = 25;
389: assertEquals(area.getMinimumSize(rows, cols), area.minimumSize(
390: rows, cols));
391: }
392:
393: /*
394: * Test method for 'java.awt.TextArea.preferredSize(int, int)'
395: */
396: @SuppressWarnings("deprecation")
397: public void testPreferredSizeIntInt() {
398: frame = new Frame();
399: frame.add(area);
400: frame.addNotify();
401: int rows = 10;
402: int cols = 25;
403: assertEquals(area.getPreferredSize(rows, cols), area
404: .preferredSize(rows, cols));
405: }
406:
407: /*
408: * Test method for 'java.awt.TextArea.replaceRange(String, int, int)'
409: */
410: public void testReplaceRange() {
411: int start = 8;
412: int end = 11;
413: String text = "This is old text";
414: area.setText(text);
415: String str = "brand new";
416: area.replaceRange(str, start, end);
417: assertEquals("This is brand new text", area.getText());
418: assertEquals("", area.getSelectedText());
419: assertEquals(0, area.getRows());
420: assertEquals(0, area.getCaretPosition());
421: }
422:
423: /*
424: * Test method for 'java.awt.TextArea.replaceText(String, int, int)'
425: */
426: @SuppressWarnings("deprecation")
427: public void testReplaceText() {
428: String text = "This is old text";
429: area.setText(text);
430: String str = "new\n";
431: area.replaceText(str, 8, 12);
432: assertEquals("This is new\ntext", area.getText());
433: assertEquals(0, area.getColumns());
434: assertEquals(0, area.getCaretPosition());
435: }
436:
437: /*
438: * Test method for 'java.awt.TextArea.setColumns(int)'
439: */
440: public void testSetColumns() {
441: int cols = 80;
442: area.setColumns(cols);
443: assertEquals(cols, area.getColumns());
444: try {
445: area.setColumns(-1);
446: } catch (IllegalArgumentException iae) {
447: assertEquals(cols, area.getColumns());
448: return;
449: }
450: fail("no exception was thrown!");
451:
452: }
453:
454: /*
455: * Test method for 'java.awt.TextArea.setRows(int)'
456: */
457: public void testSetRows() {
458: int rows = 25;
459: area.setRows(rows);
460: assertEquals(rows, area.getRows());
461: try {
462: area.setRows(-1);
463: } catch (IllegalArgumentException iae) {
464: assertEquals(rows, area.getRows());
465: return;
466: }
467: fail("no exception was thrown!");
468: }
469:
470: }
|