001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.app.test;
031:
032: import nextapp.echo2.app.Alignment;
033: import nextapp.echo2.app.Extent;
034: import nextapp.echo2.app.IllegalChildException;
035: import nextapp.echo2.app.Label;
036: import nextapp.echo2.app.TextField;
037: import nextapp.echo2.app.text.StringDocument;
038: import junit.framework.TestCase;
039:
040: /**
041: * Unit test(s) for the <code>nextapp.echo2.app.TextField</code> and
042: * <code>nextapp.echo.TextComponent</code> components.
043: */
044: public class TextFieldTest extends TestCase {
045:
046: /**
047: * Test empty constructor and verify defaults.
048: */
049: public void testEmptyConstructor() {
050: TextField textField = new TextField();
051: assertNotNull(textField.getDocument());
052: assertEquals(StringDocument.class, textField.getDocument()
053: .getClass());
054: }
055:
056: /**
057: * Attempt to illegally add children, test for failure.
058: */
059: public void testIllegalChildren() {
060: TextField textField = new TextField();
061: boolean exceptionThrown = false;
062: try {
063: textField
064: .add(new Label(
065: "you can't add children to this component, right?"));
066: } catch (IllegalChildException ex) {
067: exceptionThrown = true;
068: }
069: assertTrue(exceptionThrown);
070: }
071:
072: /**
073: * Test receiving input from client.
074: */
075: public void testInput() {
076: TextField textField = new TextField();
077: textField.processInput(TextField.TEXT_CHANGED_PROPERTY,
078: "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
079: assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZ", textField
080: .getDocument().getText());
081: }
082:
083: /**
084: * Test primary constructor.
085: */
086: public void testPrimaryConstructor() {
087: TextField textField = new TextField(new StringDocument(),
088: "text", 30);
089: assertEquals("text", textField.getDocument().getText());
090: assertEquals(new Extent(30, Extent.EX), textField.getWidth());
091: }
092:
093: /**
094: * Ensure large text is trimmed if MaximumLength property is et.
095: */
096: public void testMaxLengthTrim() {
097: TextField textField = new TextField();
098: textField.setMaximumLength(5);
099: textField.setText("abcdefghijkl");
100: assertEquals("abcde", textField.getText());
101: }
102:
103: /**
104: * Test property accessors and mutators.
105: */
106: public void testProperties() {
107: TextField textField = new TextField();
108: textField.setAlignment(new Alignment(Alignment.LEADING,
109: Alignment.BOTTOM));
110: assertEquals(
111: new Alignment(Alignment.LEADING, Alignment.BOTTOM),
112: textField.getAlignment());
113: textField.setBorder(TestConstants.BORDER_THICK_ORANGE);
114: assertEquals(TestConstants.BORDER_THICK_ORANGE, textField
115: .getBorder());
116: textField.setHeight(TestConstants.EXTENT_30_PX);
117: assertEquals(TestConstants.EXTENT_30_PX, textField.getHeight());
118: textField.setInsets(TestConstants.INSETS_1234);
119: assertEquals(TestConstants.INSETS_1234, textField.getInsets());
120: textField.setWidth(TestConstants.EXTENT_100_PX);
121: assertEquals(TestConstants.EXTENT_100_PX, textField.getWidth());
122: textField.setBackgroundImage(TestConstants.BACKGROUND_IMAGE);
123:
124: assertEquals(TestConstants.BACKGROUND_IMAGE, textField
125: .getBackgroundImage());
126: assertEquals(-1, textField.getMaximumLength());
127: textField.setMaximumLength(20);
128: assertEquals(20, textField.getMaximumLength());
129: textField.setMaximumLength(-1);
130: assertEquals(-1, textField.getMaximumLength());
131: }
132: }
|